2

我使用来自不同端口/域的 AJAX 向 YAWS 服务器发送一个发布请求,但 javascript 返回此错误消息:

XMLHttpRequest cannot load http://0.0.0.0:8000/index.yaws. Origin http://localhost is not allowed by Access-Control-Allow-Origin.

现在我知道我需要在index.yaws文件中包含 CORS 标头,但我不知道如何在Erlang.

4

1 回答 1

1

如果您只想设置localhost为允许的来源,您可以尝试下面的代码。请注意,它表示您当前使用变量返回的 JSON 结果YourJsonString

out(Arg) ->
    Hdrs = yaws_api:arg_headers(Arg),
    case yaws_api:get_header(Hdrs, "Origin") of
        "localhost" ->
            [{header, {"Access-Control-Allow-Origin", "localhost"}},
             {html, YourJsonString}];
        _ ->
            {html, YourJsonString}
    end.
于 2016-05-11T20:18:56.837 回答