0

I'm picking up on someone else work with a custom template, and I'm getting the following error:

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.

Here is the code that sends the AJAX request:

$.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();
            }
          });

I've had a similar problem with CORS before, but I haven't seen the preflight error before. Can anyone help, please?

Reza Mousavi
  • 3,603
  • 5
  • 21
  • 39
Mat
  • 895
  • 8
  • 17
  • Possible duplicate of [CORS - How do 'preflight' an httprequest?](https://stackoverflow.com/questions/8685678/cors-how-do-preflight-an-httprequest) – darklightcode Oct 01 '18 at 20:09

1 Answers1

0

please remove the Access-Control-Allow-Origin header from request header. that is a server side header.

Then add Content-Type as,

application/x-www-form-urlencoded,multipart/form-data or text/plain depending on what you are sending with the request. for example,

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

The only allowed values for the Content-Type header are:

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

for more info, read the MDN DOC.

Hope It helps. good luck.

Sankha Karunasekara
  • 1,328
  • 13
  • 19
  • Thanks, but this doesn't fix the error I'm getting - Failed to load https://api.worldpay.com/v1/orders: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin '{IPADDRESSHERE}' is therefore not allowed access. – Mat Oct 01 '18 at 09:43
  • I just check the apo docs, according to doc Content type shoud be application/json. try adding "Content-type: application/json". It shoud work. – Sankha Karunasekara Oct 01 '18 at 15:44