我一直在尝试使用 bing 拼写检查 api 来实现拼写检查。我收到 401 错误。错误是
请求的资源上不存在“Access-Control-Allow-Origin” header 。因此不允许访问 Origin 'null'。响应具有 HTTP 状态代码 401。
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
<script type="text/javascript">
function hi(){
var params = {
// Request parameters
"mode": "{Spell}",
};
$.ajax({
url: "https://api.cognitive.microsoft.com/bing/v5.0/spellcheck/?" + $.param(params),
beforeSend: function(xhrObj){
// Request headers
xhrObj.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
xhrObj.setRequestHeader("Ocp-Apim-Subscription-Key","{0c6cb56a997b41d4958ace895a677729}");
},
crossDomain: false,
headers: {'X-Requested-With': 'XMLHttpRequest'},
type: "POST",
dataType: 'jsonp',
// Request body
data: {
dataSpell: $("#textContent").val()},
success : function(result){
$("div").html(result); }
//dataType: 'jsonp'
});
}
</script>
</head>
<body>
<textarea rows="4" cols="50" id="textContent" spell-check="true" placeholder="Type here"></textarea>
<button type="button" id="spellcheck" onclick="hi();" >Click Me!</button>
<div> </div>
</body>
</html>
最佳答案
{...} you are doing an XMLHttpRequest to a different domain than your page is on. So the browser is blocking it as it usually allows a request in the same origin for security reasons. You need to do something different when you want to do a cross-domain request.
引用来自 this thread 的回答,所以如果您仍然对为什么会收到此错误感到困惑,请检查一下。您可以使用 CORS 了解如何绕过此问题在这里。
关于javascript - 请求的资源上不存在 'Access-Control-Allow-Origin' header 。 401错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38519144/