Serverless CORS Error: Did not find method in CORS header ‘Access-Control-Allow-Methods'

Serverless CORS Error: Did not find method in CORS header ‘Access-Control-Allow-Methods'

我尝试使用无服务器框架并按照指南启用了 CORS。

const response = {
    statusCode: 200,
    headers: {
      'Access-Control-Allow-Origin': '*'
    },  
    body: JSON.stringify({
        temperature: 30,
        locationId: event.queryStringParameters || event.queryStringParameters.id
    })
  };

而且我还在serverless.yml中添加了。

functions:
  getListComment:
    handler: handler.getListComment
    events:
        - http:
            path: comments/list
            method: get
            cors: true

终点站在这里

https://dgyoawr9n0.execute-api.us-east-1.amazonaws.com/dev/comments/list

当我尝试通过 AJAX 调用端点时有点问题。

    $.ajax({
        url: 'https://dgyoawr9n0.execute-api.us-east-1.amazonaws.com/dev/comments/list',
        type: 'json',
        crossDomain: true,
        contentType: "application/json",
        success: function(data) {
            alert('test');
        }
    });

没有任何反应。

Firefox 控制台显示此消息

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://dgyoawr9n0.execute-api.us-east-1.amazonaws.com/dev/comments/list. (Reason: Did not find method in CORS header ‘Access-Control-Allow-Methods’).

并且 Chrome 控制台也显示了这个

XMLHttpRequest cannot load https://dgyoawr9n0.execute-api.us-east-1.amazonaws.com/dev/comments/list. Method JSON is not allowed by Access-Control-Allow-Methods in preflight response.

但是当我尝试 http://www.test-cors.org 中的端点时,它看起来还不错。

Sending GET request to https://dgyoawr9n0.execute-api.us-east-1.amazonaws.com/dev/comments/list

Fired XHR event: loadstart
Fired XHR event: readystatechange
Fired XHR event: readystatechange
Fired XHR event: progress
Fired XHR event: readystatechange
Fired XHR event: load

XHR status: 200
XHR status text: OK
XHR exposed response headers:

    Content-Type: application/json

这是对 OPTIONS

的回复 header
HTTP/1.1 200 OK
Content-Type: application/json
Content-Length: 0
Connection: keep-alive
Date: Sat, 17 Dec 2016 02:16:30 GMT
Access-Control-Allow-Headers: Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token
Access-Control-Allow-Methods: OPTIONS,GET
Access-Control-Allow-Origin: *
x-amzn-RequestId: d2ab4dce-c3fe-11e6-bcee-6767a7211424
X-Cache: Miss from cloudfront
Via: 1.1 c038088d4b94486d7346fd44d03188a0.cloudfront.net (CloudFront)
X-Amz-Cf-Id: 30nHstKUhLwnHDwYEF3VdugR3JsuXHvUScBRDRFHRhimPW_DHS7RPQ==

这里是 GET

HTTP/1.1 200 OK
Content-Type: application/json
Content-Length: 158
Connection: keep-alive
Date: Sat, 17 Dec 2016 02:17:17 GMT
Access-Control-Allow-Methods: OPTIONS,GET
Access-Control-Allow-Origin: *
x-amzn-RequestId: ee6c19a6-c3fe-11e6-a0dc-01a17c495e09
X-Amzn-Trace-Id: Root=1-5854a02d-ba86e18abb4d47eb5094343b
X-Cache: Miss from cloudfront
Via: 1.1 18101d17be4ee51b5a03b68cfed50445.cloudfront.net (CloudFront)
X-Amz-Cf-Id: 8K8NhwXGzhqR4bYLSFrRglogJQmQq3D3GJ2P4FrYoO-naDs-I55haA==

我做错了什么?

您正在使用 'json' 的请求方法(通过 type 属性)发出请求; 'json' 不是有效的 request method。而不是 type: 'json' 你可能想要 dataType: 'json'.

type 属性 可用于指定 request method(例如 'GET'),尽管它已被弃用,取而代之的是 method 属性 自 jquery v1.9.

$.ajax({
url: 'https://dgyoawr9n0.execute-api.us-east-1.amazonaws.com/dev/comments/list',
method: 'GET', // not necessary since it will default to 'GET'
dataType: 'json', // use dataType, not type, to specify expected response data type
crossDomain: true,
contentType: "application/json",
success: function(data) {
    alert('test');
}});