繁体   English   中英

XMLHttpRequest无法加载,不存在“ Access-Control-Allow-Origin”标头(无法在JavaScript / jQuery中读取Ajax响应)

[英]XMLHttpRequest cannot load, No 'Access-Control-Allow-Origin' header is present (Unable to read Ajax response in JavaScript/jQuery )

我正在使用jQuery向服务器发送Ajax请求,下面是代码片段。

$.ajax({                
                type: 'POST',
                url: "https://store.example.com/account?",                
                data: "cf=cw&operation=update"
            })

无疑,“被请求页面”和“ Ajax请求的URL”在同一域中。 不过,我仍在浏览器控制台中看到指定的错误消息,并且无法读取JavaScript / jQuery的响应。 请在这里帮助我。

错误消息:XMLHttpRequest无法加载https://store.example.com/account ?。 所请求的资源上没有“ Access-Control-Allow-Origin”标头。 因此,不允许访问来源“ http://store.example.com ”。

仅当发送方和接收方的端口,协议和域相等时,才可能进行AJAX请求,否则可能导致CORS。 CORS代表跨域资源共享,并且必须在服务器端得到支持。

JSONP

JSONP或“带填充的JSON”是一种在网络浏览器中运行的JavaScript程序中使用的通信技术,用于从其他域中的服务器请求数据,由于相同的来源策略,典型的网络浏览器禁止使用这种方法。

$.ajax({
    type: 'POST',
    url: "https://store.example.com/account?",
    dataType: 'jsonp',
    success: function (data) {
        //code
    }
});

希望这能给你一个想法伴侣.. :)

请求页面和请求的URL是否在同一域中? 您可以尝试使用相对路径访问url吗?

$.ajax({                
            type: 'POST',
            url: "/account?",                
            data: "cf=cw&operation=update"
        })

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM