“访问控制允许来源”标题包含多个值(Windows上的Plesk)

b5buobof  于 2023-01-27  发布在  Windows
关注(0)|答案(1)|浏览(171)

我在JS中有以下代码:

// Function to feed the API
function FeedAPI() {
    // Define the API endpoint URL
    var apiUrl = "https://ifthenpay.com/api/gateway/paybylink/XXX-XXXX";

    // Define the JSON API body
    var apiData = {
        "id": "1234",
        "amount": ValueSelected(),
        "description": $('#name').val(),
        "lang": "en",
        "expiredate": "",
        "accounts": "",
        "selected_method": PaymentSelected()
    };
    console.log("API data: ", apiData);
    
    // Use jQuery's $.ajax() function to make the request
    $.ajax({
        type: "POST",
        url: apiUrl,
        data: JSON.stringify(apiData),
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(response) {
            console.log("API response: ", response);
        },
        error: function(xhr, status, error) {
            console.error("Error: ", error);
        }
    });
}

但得到这个错误:Access to XMLHttpRequest at 'https://ifthenpay.com/api/gateway/paybylink/XXX-XXXX' from origin 'https://example.com' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: The 'Access-Control-Allow-Origin' header contains multiple values '*, *', but only one is allowed.
我正在Windows上使用Plesk,并且已经尝试将web.config文件设置为以下内容:

<httpProtocol>
  <customHeaders>
    <add name="Access-Control-Allow-Origin" value="*" />
    <add name="Access-Control-Allow-Headers" value="Content-Type" />
    <add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE, OPTIONS" />
    <add name="Access-Control-Allow-Credentials" value="true" />
  </customHeaders>  
</httpProtocol>

在这一点上,我尝试了一切...有人能帮忙吗?

lb3vh1jj

lb3vh1jj1#

所以,我通过将API集成的方法更改为"GET"来修复我的问题。
下面是代码:

function FeedAPI() {
// Define the API endpoint URL
var apiUrl = "https://ifthenpay.com/api/gateway/paybylink/get";

// Define the query parameters
var apiParams = {
    "gatewaykey": "XXX-XXXX",
    "id": "1234",
    "amount": ValueSelected(),
    "description": $('#name').val(),
    "lang": "en",
    "expiredate": "",
    "accounts": "XXX|YYY;MBWAY|XXX-XXXXX",
    "selected_method": PaymentSelected()
};

console.log("API params: ", apiParams);

// Use jQuery's $.ajax() function to make the request
$.ajax({
    type: "GET",
    url: apiUrl,
    data: apiParams,
    success: function(response) {
        console.log("API response: ", response);
        window.open(response.link, "_blank");
    },
    error: function(xhr, status, error) {
        console.error("Error: ", error);
    }
});
}

相关问题