簡體   English   中英

如何將 Access-Control-Allow-Origin 標頭添加到 yaws 文件?

[英]How to add Access-Control-Allow-Origin headers to yaws file?

我使用 AJAX 從不同的端口/域向 YAWS 服務器發送了一個 post 請求,但 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做到這一點。

如果您只想將localhost設置為允許的來源,您可以嘗試以下代碼。 請注意,它表示您當前使用變量YourJsonString返回的 JSON 結果。

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.

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM