iamfredrik
iamfredrik

Reputation: 340

axios patch not working in chrome

I'm trying to update some articles on a drupal site using React axios. GET and POST requests work, but I can't get PATCH to work. Sending the PATCH request through Postman works just fine. I get the following error using axios:

Method PATCH is not allowed by Access-Control-Allow-Methods in preflight response.

What am I doing wrong?

Here's my code:

  editState(nid, value){
    let node = {
      "type":[{"target_id":"article","target_type":"node_type"}],
      "body":[{"value": value}]
    }
    let config = {
      headers: { 'Content-Type': 'application/json' }
    }
    axios.patch('http://localhost:8888/d8restapi/node/' + nid + '?_format=json',node,config)
    .then((success) => {
      console.log(success);
    }).catch((error) => {
      console.log(error);
    });
  }

Here are my OPTIONS request Headers:

Edit: Tested with Firefox and it works, but it doesn't work in Chrome! Have emptied caches and restarted and done a hard reload to no avail.

Edit 2: Problem solved. I installed another CORS plugin in Chrome. The following works in case anyone is facing the same problem with other CORS plugins:

https://chrome.google.com/webstore/detail/moesif-origin-cors-change/digfbfaphojjndkpccljibejjbppifbc

Upvotes: 2

Views: 11767

Answers (2)

surga
surga

Reputation: 1671

I've similar experience on serverless and reactjs. First I thought the issue is on serverless CORS configuration but it's actually I forgot to put

e.preventDefault()

when submitting the form. As a result, the request is cancelled because the page is loaded although the response is not returned yet.

Upvotes: 0

miu
miu

Reputation: 1304

I've experienced exactly the same thing. Like you, just the combination of react, axios and chrome didn't work.

I solved the issue by changing my CORS chrome extension to this one: https://chrome.google.com/webstore/detail/access-control-allow-cred/hmcjjmkppmkpobeokkhgkecjlaobjldi

Upvotes: 2

Related Questions