CORS不允许一个XHR而不允许另一个XHR到同一个域

时间:2016-07-05 23:26:48

标签: javascript jquery xmlhttprequest cors

我尝试使用api.ai创建浏览器内应用。它涉及两个XMLHttpRequest,只允许其中一个。{1}}。他们都去了同一个域。工作请求使用JQuery,但非工作请求使用纯XHR,因为JQuery不支持blob。

工作要求:

        $.ajax({
            type: "POST",
            url: baseUrl + "query/",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            headers: {
                "Authorization": "Bearer " + accessToken
            },
            data: JSON.stringify({ q: text, lang: "en" }),
           //Parse response etc.
        });

非工作请求:

        var xhr = new XMLHttpRequest();
        xhr.open("GET", baseUrl + "tts?text=" + text, true);
        xhr.responseType = "blob";
        xhr.setRequestHeader("Authorization","Bearer " + accessToken);
        xhr.setRequestHeader("Accept-language","en-US");
        xhr.onload = function(e) {
            //Parse response etc
        }
        xhr.send()

这个显示错误XMLHttpRequest cannot load https://api.api.ai/v1/tts?text=test. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'https://www.k7dxs.xyz' is therefore not allowed access. The response had HTTP status code 405.这似乎是因为CORS,但为什么另一个有效?我在这个问题上做错了吗?

0 个答案:

没有答案