【问题标题】:how to retrieve data from a website returning Json data如何从返回 Json 数据的网站中检索数据
【发布时间】:2015-05-07 10:46:56
【问题描述】:

我正在尝试从返回 JSON 格式数据的网页中检索数据。我正在使用简单的 ajax Jquery 来做同样的事情

$(document).ready(function() {
    /*Project Db start Here*/
    $.ajax({
        url: 'http://enlytica.com/RSLivee/EnClient',
        type: 'GET',
        dataType: "JSONP",
        crossDomain: true,
        success: function(data) {
            //called when successful
            alert("success");
        },
        error: function(e) {
            //called when there is an error
            //console.log(e);
            alert(e);
        }
    });
});

但我的 html 页面出现以下错误:

  XMLHttpRequest cannot load http://local.html.
  No 'Access-Control-Allow-Origin' header is present on the requested
  resource. Origin 'null' is therefore not allowed access.

我怎样才能解决这个问题并从链接中获取数据。 提前致谢

【问题讨论】:

    标签: jquery json web-services jquery-ajaxq


    【解决方案1】:

    您将无法向该链接发送请求,因为它位于不同的域中。

    由于页面和目标 URL 位于不同的域中,您的代码实际上是在尝试发出跨域 (CORS) 请求,而不是普通的 GET 或 POST 请求。

    同源策略将允许浏览器对同一域中的服务进行 ajax 调用。

    为您的请求提供服务的文件必须发送此标头

    Access-Control-Allow-Origin: *
    

    如果此标头不是由服务器发送的文件,浏览器将无法执行任何操作。

    查看链接如何启用 CORS 请求

    http://enable-cors.org/server.html

    【讨论】:

    • 谢谢它的帮助!!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-02-08
    • 1970-01-01
    • 1970-01-01
    • 2010-11-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多