简体   繁体   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 )

I'm sending an Ajax request to server using jQuery, below is the code snippet. 我正在使用jQuery向服务器发送Ajax请求,下面是代码片段。

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

Definitely, “requested page” and “Ajax request's URL” are in same domain. 无疑,“被请求页面”和“ Ajax请求的URL”在同一域中。 Still, Am seeing specified error message in browser console and unable read response with JavaScript/jQuery. 不过,我仍在浏览器控制台中看到指定的错误消息,并且无法读取JavaScript / jQuery的响应。 Please help me here. 请在这里帮助我。

Error message: XMLHttpRequest cannot load https://store.example.com/account ?. 错误消息:XMLHttpRequest无法加载https://store.example.com/account ?。 No 'Access-Control-Allow-Origin' header is present on the requested resource. 所请求的资源上没有“ Access-Control-Allow-Origin”标头。 Origin ' http://store.example.com ' is therefore not allowed access. 因此,不允许访问来源“ http://store.example.com ”。

AJAX Requests are only possible if port, protocol and domain of sender and receiver are equal,if not might lead to CORS. 仅当发送方和接收方的端口,协议和域相等时,才可能进行AJAX请求,否则可能导致CORS。 CORS stands for Cross-origin resource sharing and has to be supported on the server side. CORS代表跨域资源共享,并且必须在服务器端得到支持。

Solution

JSONP JSONP

JSONP or "JSON with padding" is a communication technique used in JavaScript programs running in web browsers to request data from a server in a different domain, something prohibited by typical web browsers because of the same-origin policy. JSONP或“带填充的JSON”是一种在网络浏览器中运行的JavaScript程序中使用的通信技术,用于从其他域中的服务器请求数据,由于相同的来源策略,典型的网络浏览器禁止使用这种方法。

Example

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

Hope this gives you an idea mate.. :) 希望这能给你一个想法伴侣.. :)

Are the requesting page and requested URL are in the same domain. 请求页面和请求的URL是否在同一域中? Can you try accessing the url using relative path? 您可以尝试使用相对路径访问url吗?

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

暂无
暂无

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

相关问题 XMLHttpRequest无法加载。 不存在“ Access-Control-Allow-Origin”标头。 (阿帕奇/ jQuery的/ AJAX) - XMLHttpRequest cannot load. No 'Access-Control-Allow-Origin' header is present. (Apache/jQuery/AJAX) XMLHttpRequest无法加载“…所请求的资源上没有'Access-Control-Allow-Origin'头。” — jQuery - XMLHttpRequest cannot load “ …No 'Access-Control-Allow-Origin' header is present on the requested resource.” — Jquery XMLHttpRequest无法加载[archivo]。 所请求的资源上没有“ Access-Control-Allow-Origin”标头。 起源[dominio] - XMLHttpRequest cannot load [archivo]. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin [dominio] XMLHttpRequest无法加载数据。 请求的资源上不存在“ Access-Control-Allow-Origin”标头 - XMLHttpRequest cannot load data. No 'Access-Control-Allow-Origin' header is present on the requested resource Wampserver XMLHttpRequest无法加载所请求的资源上不存在“ Access-Control-Allow-Origin”标头 - Wampserver XMLHttpRequest cannot load No 'Access-Control-Allow-Origin' header is present on the requested resource 无法为No'Access-Control-Allow-Origin'标头加载XMLHttpRequest - XMLHttpRequest cannot load for No 'Access-Control-Allow-Origin' header XMLHttpRequest无法加载…所请求的资源上没有“ Access-Control-Allow-Origin”标头 - XMLHttpRequest cannot load…No 'Access-Control-Allow-Origin' header is present on the requested resource XMLHttpRequest无法加载。 请求的资源上不存在“Access-Control-Allow-Origin”标头。 因此不允许原点访问 - XMLHttpRequest cannot load. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin is therefore not allowed access 无法使此jQuery AJAX + JSONP正常工作。 “不存在'Access-Control-Allow-Origin'标头” - Cannot get this jQuery AJAX + JSONP to work. “No 'Access-Control-Allow-Origin' header is present” XMLHttpRequest无法加载在请求的资源上不存在“ Access-Control-Allow-Origin”标头。 AWS S3,Retina.JS - XMLHttpRequest cannot load No 'Access-Control-Allow-Origin' header is present on the requested resource. AWS S3, Retina.JS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM