我有一些 Ajax 可以进行跨源资源调用
$("#inductive1").click(function (event) {
$.post(
"https://www.mysite.co.uk/folder/tests/Inductive/Test1/index.phtml",
function (data) {
$('.stage2').html(data);
}
);
});
在 index.phtml
文件中,我有一些调用 exam.xml
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));
但是 xml 文件正尝试从其他服务器调用,
EG
https://server1.com/exam.xml
应该是
https://myserver.com/exam.xml
我尝试将 JS 更改为直接路径,如
XMLFile: "htttps:/myserver.com/exam.xml"
但它被读作
https://server1.com/myserver.com/exam.xml
如何更改 javascript,以便将根 URL 更改为 myserver.com
而不是 server1.com
最佳答案
看起来您正在使用的某些插件正在更改文档的基本 URL, 你可以尝试将其放在index.phtml的开头
<base href="https://myserver.com">
如果这不起作用,您可能需要在 ajax 调用之前通过 JavaScript 动态更改基本属性
document.write("<base href='http://myserver.com/'>");
关于javascript - 带有 XML 调用的 CORS 发布索引文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40152521/