Method PATCH is not allowed by Access-Control-Allow-Methods in preflight response
Asked Answered
I

6

8

I am using axios PATCH method in ReactJS to update the record but its getting failed with following error

Failed to load http://192.168.99.100:8080/adslots/883: Method PATCH is not allowed by Access-Control-Allow-Methods in preflight response.

Here is my action:

export const UPDATE_AD_SLOTS_REQUEST = 'UPDATE_AD_SLOTS_REQUEST';
export const UPDATE_AD_SLOTS_SUCCESS = 'UPDATE_AD_SLOTS_SUCCESS';
export const UPDATE_AD_SLOTS_ERROR = 'UPDATE_AD_SLOTS_ERROR';


export function updateAdslotsRequest(){
  return {
    type: UPDATE_AD_SLOTS_REQUEST
  }
}

export function updateAdslotsSuccess(data){
  return {
    type: UPDATE_AD_SLOTS_SUCCESS,
    data: data
  }
}

export function updateAdslotsError(errors){
  return {
    type: UPDATE_AD_SLOTS_ERROR,
    erros: errors
  }
}

export function updateAdslots(data, id) {
  return dispatch => {
    dispatch(updateAdslotsRequest());
    return axios.patch(`http://192.168.99.100:8080/adslots/${id}`, data)
      .then(res => {
        dispatch(updateAdslotsSuccess(res.data));      
      })
      .catch(errors => {
        dispatch(updateAdslotsError(errors));
      })
  }
}

I am totally confused.

Iene answered 26/5, 2018 at 8:50 Comment(0)
L
6

Try this solution:

  1. Go to your app.js file where you've defined all the middleware.

  2. Open terminal and type command "npm install cors", and hit enter.

  3. Now write the following code in your file:

    const cors = require("cors");
    
    const app = express();
    
    app.use(cors());
    

Hopefully, It will do the trick!

Lithography answered 1/1, 2021 at 11:58 Comment(5)
It's worked for me,Handgun
You helped me a lot with your solution! Thanks!Daleth
you're welcome :') @DalethLithography
For worktop users, just do: ``` import { Router, compose } from 'worktop'; import { preflight } from 'worktop/cors'; const router = new Router(); router.prepare = compose(preflight()); ```Mattos
it's not working for me :'(Marsden
A
5

The api you are making the call to has to allow PATCH requests. They can do this by setting the Access-Control-Allow-Methods header to also have Patch in it. Look up how to do this with whatever server side language your api is using. You could also maybe try switching your call to a POST request but that is more of a temporary fix.

Aglow answered 26/5, 2018 at 9:2 Comment(1)
I have this and it doesn't work for me and can't find anything which solves my problem... I'm opening a new question since this one seems to be abandonedHaletta
B
4

I think it is problem related to CORS settings on your backend. You have to allow PATCH requests in CORS settings. What kind of backend server are you using?

Blithering answered 26/5, 2018 at 8:55 Comment(0)
H
1

You can use any cors extension/plugin to make it work in browsers. Also, make sure u have configured extension settings sometimes PATCH requests are not listed down in extension settings

Happy to help !

Hardison answered 26/2, 2021 at 9:28 Comment(0)
D
1

I also encountered the same error while making the patch request. I tried everything from configuring CORS on server side to add patch in allowed methods. But none of the solutions work. Finally I replaced it with PUT request and it worked fine.

Dialectics answered 31/7 at 14:51 Comment(0)
P
-1

workaround : use browserplugin CORS (chrome) when cors is activated you can open cors options and add localhost:3000 to the whitelist. Then this thing is working for me

Portend answered 7/12, 2022 at 14:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.