我正在尝试使用Ajax调用发出POST请求,但是状态代码为0时出现错误,因为所有请求参数都在Advanced REST Client中工作。
我的代码:
<button>Post</button>
$('button').click(function(e) {
e.preventDefault();
$.ajax({
url: "https://api.sendgrid.com/v3/mail/send",
type: "POST",
contentType : 'application/json',
headers : {
'Authorization' : 'Bearer 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',
'Content-Type' : 'application/json'
},
data: JSON.stringify({
"personalizations": [{
"to": [{
"email": "[email protected]"
}]
}],
"from": {
"email": "[email protected]"
},
"subject": "Great, World!",
"content": [{
"type": "text/plain",
"value": "Cool OK!"
}]
}),
dataType: "json",
success: function (response) {
var resp = JSON.parse(response)
alert(resp.status);
},
error: function (xhr, status) {
alert("error : "+status+" :: "+JSON.stringify(xhr));
}
});
});
这是我上面的代码,在运行时不断抛出错误说
{"readyState":0,"responseText":"","status":0,"statusText":"error"}
还要检查jsfiddle
请帮我,建议我一些解决方案,我无法在代码中找到问题。
浏览器扩展不像浏览器本身那样局限于跨源请求。因此,允许诸如Advance Rest Client或Postman之类的扩展程序请求跨源资源。请参阅Chrome中的这篇文章:https : //developer.chrome.com/extensions/xhr
因此,当您创建一个网站要访问其他域的资源时,就像Matt所说的那样,您必须启用CORS。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句