请求的资源上不存在“Access-Control-Allow-Origin”标头。 401错误

[英]No 'Access-Control-Allow-Origin' header is present on the requested resource. 401 error


I have been trying to implement spell check by using the bing spell check api. I am getting a 401 error. The error is

我一直试图通过使用bing拼写检查api来实施拼写检查。我收到401错误。错误是

No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access. The response had HTTP status code 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>​

1 个解决方案

#1


1  

{...} 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.

{...}您正在对与您的页面所在的域不同的域执行XMLHttpRequest。因此浏览器阻止它,因为出于安全原因,它通常允许同一来源的请求。当您想要执行跨域请求时,您需要执行不同的操作。

Quoting answer from this thread, so check it out if you are still confused about why you are getting this error. You can learn how to bypass this using CORS here.

引用这个帖子的答案,如果你仍然对你收到此错误的原因感到困惑,请查看它。您可以在此处了解如何使用CORS绕过此问题。

智能推荐

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.silva-art.net/blog/2016/07/22/17846e4272da737ca1ff342316e90e51.html



 
© 2014-2019 ITdaan.com 粤ICP备14056181号  

赞助商广告