简体   繁体   English

React axios put 请求返回错误 500

[英]React axios put request returns error 500

axios.put('http://1.1.1.1:1010/api/data', {
    data: {
        ip: '123123',
        smth: true
    },
    headers: {
        'content-type': "application/json ",
        'Access-Control-Allow-Origin': '*',
    },
}).then(res => {
    console.log(res)
}).catch(error => {
    console.log(error);
})

I recieved我收到了

Error: Request failed with status code 500

Get request works fine.获取请求工作正常。 I tryed without any success:我尝试没有任何成功:

headers: {
    'content-type': "application/json ",
    'Access-Control-Allow-Origin': '*',
},

data: {
    ip: '123123',
    dhcp: true
},
withCredentials: true,
crossdomain: true,
proxy: {
    host: 'http://1.1.1.1',
    port: 1010
},
validateStatus: (status) => {
    return true; 
},

In the backend error on the last line::在最后一行的后端错误中:

json_data = request.get_json(force=True)
accountGuid = 'DummyAccount'
pdict = json_data[self.tableName]

Help me please.请帮帮我。 I don't understood what I have to do我不明白我必须做什么

OK I'm not sure what the logic is behind this but it recently happened to me.好的,我不确定这背后的逻辑是什么,但最近发生在我身上。 I had a put request that was giving me 500 internal server error but on postman the same request would work fine.我有一个 put 请求,它给了我 500 个内部服务器错误,但在邮递员上,同样的请求可以正常工作。

Here is what fixed the issue.这是解决问题的方法。 axios has two ways of sending request. axios 有两种发送请求的方式。

axios.put(url,body,config);

and

axios(request object);

I am sharing my code.我正在分享我的代码。 May be it helps you as well.也许它也对你有帮助。

The request was failing at this code.请求在此代码处失败。

const { data } = await axios.put(`trainer-schedule/cancel-appointment-student/${id}`, null, {
        headers: {
            'Authorization': 'Bearer ' + await AsyncStorage.getItem('auth_token'),
            'Access-Control-Allow-Origin': '*',
        }
    });

I just changed it to other way and it started working.我只是将其更改为其他方式并开始工作。

const { data } = await axios({
        url: `trainer-schedule/cancel-appointment-student/${id}`,
        headers: {
            'Authorization': 'Bearer ' + await AsyncStorage.getItem('auth_token'),
            'Access-Control-Allow-Origin': '*',
        },
        method: 'put'
    });

My envoirenment is react-native, backend express js.我的环境是 react-native,后端 express js。

I would recommend to always test your requests on postman.我建议始终在邮递员上测试您的请求。 It is the best tool out there to test apis.它是测试 api 的最佳工具。 but what happened to me is a very rare case.但发生在我身上的是一个非常罕见的案例。

I hope it helps you.我希望它能帮助你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM