WorldPay AJAX请求的CORS / Preflight错误

时间:2018-09-30 15:54:13

标签: javascript jquery ajax

我正在接洽使用自定义模板的其他人,并且遇到以下错误:

Failed to load https://api.worldpay.com/v1/orders: Response to the preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://xxx.xxx.xxx.xxx' is therefore not allowed access.

以下是发送AJAX请求的代码:

$.ajax({
            type: "POST",
            url: "https://api.worldpay.com/v1/orders",
            dataType: 'json',
            async: false,
            headers: {
              'Access-Control-Allow-Origin': '*',
              "Authorization": worldPayServiceKey
            },
            data: JSON.stringify(options),
            success: function (response){
              if (response.paymentStatus == 'SUCCESS') {
                current_booking.payment = obj.response;
              } else {
                alert("Sorry, there was a problem with your payment. We will send you an invoice instead.");
              }

              makeBooking();
            }
          });

我以前在CORS上也遇到过类似的问题,但是之前我没有看到预检错误。有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

请从请求标头中删除Access-Control-Allow-Origin标头。这是服务器端标头。

然后将Content-Type添加为

application/x-www-form-urlencodedmultipart/form-datatext/plain,取决于您随请求发送的内容。例如,

headers: {
              "Content-Type": "application/x-www-form-urlencoded",
              "Authorization": worldPayServiceKey
            },
  

Content-Type标头的唯一允许值为:

> - application/x-www-form-urlencoded
> - multipart/form-data 
> - text/plain

有关更多信息,请阅读MDN DOC

希望它会有所帮助。祝你好运。