2 回答

TA貢獻(xiàn)2039條經(jīng)驗(yàn) 獲得超8個(gè)贊
要啟用cors連接到 lambda 函數(shù)的 api 網(wǎng)關(guān)端點(diǎn),您必須為 api 端點(diǎn)啟用 cors(您已經(jīng)完成)并配置您的 lambda 函數(shù) suport cors。
按照我的例子:
// new respose class, replace for your class - ResponseClass
public class Response {
private int statusCode; // http status code
private Map<String, String> headers; // headers
private String body; // body - what you want to return to client
public Response(int statusCode, Map<String, String> headers, String body) {
this.statusCode = statusCode;
this.headers = headers;
this.body = body;
}
public int getStatusCode() {
return statusCode;
}
public Map<String, String> getHeaders() {
return headers;
}
public String getBody() {
return body;
}
public void setStatusCode(int statusCode) {
this.statusCode = statusCode;
}
public void setHeaders(Map<String, String> headers) {
this.headers = headers;
}
public void setBody(String body) {
this.body = body;
}
}
/// -------------
package lambda;
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
import lambda.axon.Path;
public class shortestPath implements RequestHandler<RequestClass, Response>{
public Response handleRequest(RequestClass request, Context context){
String inputString = request.inputString;
int steps = Path.stepsTo(inputString);
Map<String, String> headers = new HashMap<>();
headers.put(Access-Control-Allow-Origin, "*"); // cors header, you can add another header fields
return new Response(200, headers, "" + steps);
// return new Response(200, headers, "{result: " + steps + "}");
// simple json response. ex: {result: '3433"}
}
}
我的方式僅在 api 網(wǎng)關(guān)使用LAMBDA-PROXY事件配置時(shí)生效(默認(rèn))

TA貢獻(xiàn)1877條經(jīng)驗(yàn) 獲得超6個(gè)贊
您可以從 api 網(wǎng)關(guān)啟用 cors 您可以從 lambda 管理 cors 并從 lambda 管理標(biāo)頭。
我建議從 api 網(wǎng)關(guān)啟用 cors 并測(cè)試它是否可以工作。
你可以管理access-control-origin并headers喜歡這種方式
'use strict';
module.exports.hello = function(event, context, callback) {
const response = {
statusCode: 200,
headers: {
"Access-Control-Allow-Origin" : "*", // Required for CORS support to work
"Access-Control-Allow-Credentials" : true // Required for cookies, authorization headers with HTTPS
},
body: JSON.stringify({ "message": "Hello World!" })
};
callback(null, response);
};
您可以參考此文檔: https ://serverless.com/framework/docs/providers/aws/events/apigateway/
添加回答
舉報(bào)