繁体   English   中英

使用JavaScript发送预检Cors请求

[英]Sending preflight cors request in javascript

我正在尝试将POST请求发送到自定义api(我具有凭据),但始终收到401错误“ Unathorized”。 这是发出请求的代码的一部分:

var httpRequest = new XMLHttpRequest();
var test = [{
                          "action": "start",
                          "time": "2014-05-02T13:09:22+02:00",
                          "channel": {
                          "id": "rtvslo1"
                          },
                          "content": {
                            "id": "rtvslo1",
                             "title": "testContent",
                             "channel_id": "rtvslo1",
                             "time": "2014-05-02T13:09:22+02:00",
                             "duration": 3
                          },
                          "subscriber": {
                            "id": "f8721ca0-d1e9-11e3-9c1a-0800200c9a66",
                            "country": "si"
                          },
                          "device": {
                            "id": "012fa920-d1ea-11e3-9c1a-0800200c9a66",
                            "type": "tv"
                          },
                          "hd": false,
                          "test": true
                        }];

            var jsonString = JSON.stringify(test);

            httpRequest.open('POST', url, true);
            httpRequest.setRequestHeader("Authorization", "Basic " + btoa(username + ":" + password));
            httpRequest.setRequestHeader("Content-type", "application/json");
            httpRequest.send(jsonString);

当我试图查看chrome开发工具中的错误时,我得到OPTIONS请求是401 Unathorized,所以它甚至都没有进入POST。

在chrom开发工具中,我得到输出:名称:events方法:OPTIONS状态401 ...

选项http://xstreampipy.tvbeat.com/api/2.0/events 401(未经授权)myFunction @ test.html:39onclick @ test.html:60 test.html:1 XMLHttpRequest无法加载http://xstreampipy.tvbeat.com /api/2.0/events 所请求的资源上没有“ Access-Control-Allow-Origin”标头。 因此,不允许访问原始“空”。 响应的HTTP状态码为401。

任何帮助,将不胜感激 :)!

此错误基本上意味着您正在执行的其余请求是跨域的,即mydomain.com-> restservicedomain.com。 这些请求不再有效或被贬低(不知道为什么会这样做)。 有许多方法可以解决此问题。 您可以使用这些链接来了解和实施解决方案-

我也仍在尝试实现CORS请求,因此没有正在运行的代码。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM