Axios posting params not read by $_POST

前端 未结 5 827
旧时难觅i
旧时难觅i 2020-11-28 09:25

So I have this code:

axios({
    method: \'post\',
    url,
    headers: { \'Content-Type\': \'application/x-www-form-urlencoded\' },
    data: {
        jso         


        
5条回答
  •  盖世英雄少女心
    2020-11-28 09:56

    From the documentation (I haven't preserved links in the quoted material):

    Using application/x-www-form-urlencoded format

    By default, axios serializes JavaScript objects to JSON. To send data in the application/x-www-form-urlencoded format instead, you can use one of the following options.

    Browser

    In a browser, you can use the URLSearchParams API as follows:

    var params = new URLSearchParams();
    params.append('param1', 'value1');
    params.append('param2', 'value2');
    axios.post('/foo', params); 
    

    Note that URLSearchParams is not supported by all browsers, but there is a polyfill available (make sure to polyfill the global environment).

    Alternatively, you can encode data using the qs library:

    var qs = require('qs');
    axios.post('/foo', qs.stringify({ 'bar': 123 }));
    

提交回复
热议问题