我正在进行这样的ajax调用:
var username = "someusername";
var password= "somepassword";
var datam = '{"description":"my description","account":"mehame","username":"hakan","password":"11"}';
$.ajax
({
url: "http://mywebsite.com:8080/directory/send",
contentType: "application/json",
data: datam,
dataType: 'text',
type: "PUT",
headers: {
"Authorization": btoa(username + ":" + password)
},
beforeSend: function (){
//xhr.setRequestHeader('Authorization', make_base_auth(username, password));
console.log(datam);
},
success: function (){
alert("Here's lots of data, just a string: " + datam);
},
error : function(){
alert("Here's lots of data, just a string: " + datam);
}
});
});
并且firebug控制台给了我" Cross Domain Error" (并返回错误):
" 阻止跨源请求:同源策略禁止在http://mywebsite.com:8080/directory/send读取远程资源。这可以通过将资源移动到同一域或启用CORS来解决。"
但在我的Chrome扩展程序名称中," PostMan "它完全正常工作。
有什么不同?
答案 0 :(得分:0)
由于同源策略,您无法在外部主机名或端口号上执行ajax请求。 http://en.wikipedia.org/wiki/Same-origin_policy
您必须在http标头中启用CORS:http://en.wikipedia.org/wiki/Cross-origin_resource_sharing
您还可以看到此帖子知道如何执行此操作:cant get CORS working in chrome and firefox