[英]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.