XMLHttpRequest 无法加载 Origin null 是 Access-Control-Allow-Origin 不允许的
我有一个包含以下代码的 code.html 文件。
$.ajax({
type: "POST",
datatype: "JSONP",
url: "path",
success: function(msg){
var e = document.createElement("div");
e.id = "ads";
document.body.appendChild(e);
$("#ads").html(msg);
}
});
当我在浏览器中打开 code.html 文件时,出现错误:
**"XMLHttpRequest cannot load file://..... Origin null is not allowed by Access-Control-Allow-Origin."**
是什么原因导致此问题以及如何解决此问题?
I have a code.html file containing the following code.
$.ajax({
type: "POST",
datatype: "JSONP",
url: "path",
success: function(msg){
var e = document.createElement("div");
e.id = "ads";
document.body.appendChild(e);
$("#ads").html(msg);
}
});
When I open the code.html file in the browser, it gives an error:
**"XMLHttpRequest cannot load file://..... Origin null is not allowed by Access-Control-Allow-Origin."**
What is causing this and what can I do to fix this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我将做出两个假设:
然后,这个问题是 XMLHttpRequest Origin null 不允许 Access-Control-Allow-Origin for file:/// 到 file:/// (无服务器)
浏览器正在阻止跨站点脚本编写。请参阅:https://developer.mozilla.org/en-US/docs/HTTP_access_control< /a>
I will make two assumptions:
Then, this question is a duplicate of XMLHttpRequest Origin null is not allowed Access-Control-Allow-Origin for file:/// to file:/// (Serverless)
The browser is preventing cross site scripting. See: https://developer.mozilla.org/en-US/docs/HTTP_access_control
如果您的数据类型是
jsonp
(小写),则ajax类型必须是GET
而不是POST
更新:
使用$ .getJSON 而不是 $.ajax 应该可以解决您的问题
if you your dataType is
jsonp
(lowercased), the ajax type must beGET
notPOST
Update:
Use $.getJSON insteadof $.ajax should solve your problem