fetch patch request is not allowed

后端 未结 3 1338
萌比男神i
萌比男神i 2020-12-14 06:26

I have two apps one is a react front end and the second one is the rails-api app.

I have been happily using isomorphic-fetch till I needed to send PATCH method to th

3条回答
  •  忘掉有多难
    2020-12-14 06:52

    use this code _method: 'PATCH'

    return (
            fetch(API_ROOT + route, {
                _method: 'PATCH',
                crossDomain: true,
                xhrFields: {
                    withCredentials: true
                },
                headers: {
                    Accept: 'application/json',
                    'Content-Type': 'application/json',
                    'Authorization': ''
                },
                data: JSON.stringify(data),
                credentials: 'include'
            })
            .then(res => res.json())
            .then(res => {
                return res
            })
            .catch(err => console.error(err))
        );
    

    Another Way is insert method in headers

    headers: {
                Accept: 'application/json',
                'Content-Type': 'application/json',
                '_method': 'PATCH',
                'Authorization': ''
            }
    

    it helps you

    return (
            fetch(API_ROOT + route, {
                method: 'POST',
                crossDomain: true,
                xhrFields: {
                    withCredentials: true
                },
                headers: {
                    Accept: 'application/json',
                    'Content-Type': 'application/json',
                    '_method': 'PATCH',
                    'Authorization': ''
                },
                data: JSON.stringify(data)
            })
            .then(res => res.json())
            .then(res => {
                console.log(res);
                return res
            })
            .catch(err => console.error(err))
        );
    

提交回复
热议问题