Benjamin Oats Benjamin Oats - 1 month ago 10
Javascript Question

CORS post index file with XML call

I have some Ajax that makes a Cross Origin Resource call

$("#inductive1").click(function (event) {
$.post(
"https://www.mysite.co.uk/folder/tests/Inductive/Test1/index.phtml",
function (data) {
$('.stage2').html(data);
}
);
});


within the
index.phtml
file I have some Script that calls
exam.xml


Script inside index.phtml

( function($, undefined) {

$(function() {

var test = new Test({

testName: "Inductive Test 1",
dataURL : "/getresultshtml.php",
sendEmailURL: "/sendresultsbyemail.php",
contentFolder : "./",
solutionURL: "../../../content/f/id/10/",
userID: 0,
courseItemID: 25,
XMLFile: "exam.xml",
isStandalone: false
});

test.start();

});
}(jQuery));


However the xml file is trying to be called from the other server,

EG

https://server1.com/exam.xml


it should be

https://myserver.com/exam.xml


I have tried changing the JS to direct path as in

XMLFile: "htttps:/myserver.com/exam.xml"


but it is being read as

https://server1.com/myserver.com/exam.xml


how do I change the javascript so that it changes the root URL to
myserver.com
and not
server1.com

Answer

Looks like some plugin you are using is changing document's base URL, you can try putting this at the beginning of index.phtml

<base href="https://myserver.com">

If that doesn't work you might need to dynamically change the base property through JavaScript just before the ajax call

document.write("<base href='http://myserver.com/'>");