我一直试图通过使用bing拼写检查api来实施拼写检查。我收到401错误。错误是
请求的资源上没有“Access-Control-Allow-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>
答案 0 :(得分:1)
{...}您正在向与您的网页所在的域不同的域执行XMLHttpRequest。因此浏览器会阻止它,因为出于安全原因,它通常允许同一来源的请求。当您想要进行跨域请求时,您需要做一些不同的事情。
从this thread引用回答,如果您仍然对此错误的原因感到困惑,请查看它。您可以在此处使用CORS了解如何绕过此问题。