CORS请求405状态代码

时间:2018-04-24 09:01:29

标签: javascript ajax rest cors

大家好,请帮我解决一下我的CORS问题 我正在从另一个域创建一个API请求我的代码下面有一个错误,

 var headers = {
        host: host,
        path: url + instance + '?action=reset',
        method: 'POST'
    };


var request = https.request(headers, function (response) {
    logger('OCICompute', 'reset', 'INSTANCE', 'Got response from OPCAPI:' + response.statusCode);

    res.header("Access-Control-Allow-Origin", "http://localhost:3000"); 
        res.header("Access-Control-Allow-Methods", "OPTIONS, TRACE, GET, HEAD, POST, PUT");
        res.header("Access-Control-Expose-Headers", "Origin, Content-Type, Authorization, Accept, X-Requested-With");
        res.header("Access-Control-Allow-Credentials", "true");
        res.header("Access-Control-Allow-Headers", "Origin, Content-Type, Authorization, Accept, X-Requested-With"); 


    if (response.statusCode === 200) {
        response.on('data', function (body) {
            var data = JSON.parse(body);
            res.json(data);
        });
    } else {
        logger('OCICompute', 'reset', 'ERROR', 'stop fail');
        res.json({'result': 'Fail'});
    }
});

上面的代码在我的模块上

我的主要选项。

app.opts('/\.*/', function(req, res, next){
  res.header("Access-Control-Allow-Origin", "http://localhost:3000");
  res.header("Access-Control-Allow-Methods", "OPTIONS, TRACE, GET, HEAD, POST, PUT");
  res.header("Access-Control-Expose-Headers", "Origin, Content-Type, Authorization, Accept, X-Requested-With");
  res.header("Access-Control-Allow-Credentials", "true");
  res.header("Access-Control-Allow-Headers", "Origin, Content-Type, Authorization, Accept, X-Requested-With");
res.send(200);

});

当我访问此休息时,我得到了这个 405错误方法不被接受,只有这个

Host: api Url
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Firefox/52.0
Accept: */*
Accept-Language: ja,en-US;q=0.7,en;q=0.3
Accept-Encoding: gzip, deflate, br
Referer: http://localhost:3000/?root=compute
Authorization: Bearer Token
Origin: http://localhost:3000
Connection: keep-alive

Allow: OPTIONS
Connection: keep-alive
Content-Length: 58
Content-Type: application/json
Date: Tue, 24 Apr 2018 02:05:47 GMT
Server: LBAAS
Strict-Transport-Security: max-age=31536000; includeSubdomains;
X-Content-Type-Options: nosniff
X-XSS-Protection: 1; mode=block

我有另一个代码完全符合我的预期,唯一的区别是使用GET方法。上面有错误的是POST。

 var headers = {
    host: host,
    path: url + instance + '?action=reset',
    method: **'GET'**
};

这是结果

>

 Host: api Url User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0)
> Gecko/20100101 Firefox/52.0 Accept: */* Accept-Language:
> ja,en-US;q=0.7,en;q=0.3 Accept-Encoding: gzip, deflate, br Referer:
> http://localhost:3000/?root=compute Authorization: Bearer Token
> Origin: http://localhost:3000 Connection: keep-alive
> 
> Access-Control-Allow-Credentials: true Access-Control-Allow-Headers:
> Content-Type, Authorization, Accept, X-Requested-With
> Access-Control-Allow-Methods: OPTIONS, TRACE, GET, HEAD, POST, PUT
> Access-Control-Allow-Origin: http://localhost:3000
> Access-Control-Expose-Headers: Content-Type, Authorization, Accept,
> X-Requested-With Connection: keep-alive Content-Length: 2556
> Content-Type: application/json Date: Tue, 24 Apr 2018 02:05:57 GMT
> Server: LBAAS Strict-Transport-Security: max-age=31536000;
> includeSubdomains; X-Content-Type-Options: nosniff X-XSS-Protection:
> 1; mode=block

这是我在客户端的代码

self.stopButtonClick = function (event) {
                    console.log("停止ボタンが押されました");
var headers = {"Authorization": 'Bearer ' + self.ociComputeToken._latestValue};              

                    $.ajax({
                        url: url,
                        type: "GET",
                        async: true,
                        headers: headers
                    }).done(function (data, textStatus, jqXHR) {
                        if (data.result !== 'Fail') {
                            self.compartmentHandler(self.compartmentId());
                        } else {
                            self.stopButtonClick(event);
                        }
                    }).fail(function (jqXHR, textStatus, errorThrown) {
                        console.log('サービスエラー');
                        self.stopButtonClick(event);
                    });
                };

1 个答案:

答案 0 :(得分:0)

我只是解决了这个问题,问题是我访问的网址不正确,但后来又出现了另一个错误,即502坏网关,GET工作正常,但是当使用POST方法时该怎么办?