我试图将一个 html 文件的一部分加载到另一个 html 文件中,但我不断收到以下错误并且错误函数正在运行:
谁能告诉我我的 ajax 调用做错了什么?
var application = slides.names[counter].replace(/ /g, "");
$.ajax({
url: "games/" +application +"/index.html",
dataType: "HTML",
success: function(result, status, xhr){
console.log(this.url);
console.log(result);
console.log(status);
console.log(xhr);
},
error: function(result, status, xhr){
console.log(this.url);
console.log(result);
console.log(status);
console.log(xhr);
}
})
最佳答案
您不能使用 HTTP 请求本地文件,因为该协议(protocol)适用于跨源请求。
您可以通过使用 Apache(或其他本地网络服务器)在本地安装网络服务器来解决此问题:https://www.sitepoint.com/how-to-install-apache-on-windows/
或者,您可以将您尝试访问的 html 文件上传到其他位置并通过
访问它们url: "http://foo.com/" + application + "/index.html",
关于javascript - $.ajax 尝试加载 html 文件时失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42542609/