javascript - 请求的资源上不存在 'Access-Control-Allow-Origin' header 。 401错误

标签 javascript jquery html api spell-checking

我一直在尝试使用 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/

相关文章:

javascript - 没有页面滚动条的响应式 Bootstrap 模式

javascript - 如何通过 WebView 将来自 javascript 的 Alert() 操作接收到 Cocoa 应用程序

javascript - Sequelize ORM 中联接表的条件

javascript - 限制 Div 内的最大元素 - FIFO 流数据

javascript - 如何在页面加载时制作动画以将某些元素重新排列到其原始位置?

javascript - 验证月/年是否大于当前日期

javascript - 哪个更快更好?声明一个新变量或只是分配给全局变量?

javascript - Q promise : are callbacks invoked in the same order as registered?

jquery - 为 4 个测验答案按钮设置点击处理程序的简单方法是什么?

html - 事件类不能在 Bootstrap 导航栏上与 li 一起使用