【问题标题】:$.ajax failing when trying to load html file尝试加载 html 文件时 $.ajax 失败
【发布时间】:2017-03-01 21:41:11
【问题描述】:

我正在尝试将 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);
    }
})

【问题讨论】:

标签: javascript jquery ajax


【解决方案1】:

您不能使用 HTTP 请求本地文件,因为该协议适用于跨域请求。

您可以通过使用 Apache(或其他一些本地网络服务器)在本地安装网络服务器来解决此问题:https://www.sitepoint.com/how-to-install-apache-on-windows/

或者,您可以将尝试访问的 html 文件上传到其他位置并通过

url: "http://foo.com/" + application + "/index.html",

【讨论】:

    猜你喜欢
    • 2022-08-15
    • 2020-11-19
    • 2021-06-04
    • 2023-03-26
    • 1970-01-01
    • 1970-01-01
    • 2013-03-10
    • 1970-01-01
    • 2013-04-15
    相关资源
    最近更新 更多