Axios conducting parameters not readable $ _POST

So, I have this code:

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

I initially had normal axios.post , but I changed to this because I thought it might be a header problem. However, I did not find anything in $_REQUEST and $_POST . However, it accepts data in file_get_contents("php://input") .

Any idea what is wrong?

Edit

OK, I think I know what happened. It sends it as a json object, so it can only be read in php: // input. How to change it to a regular line in axios?

+5
source share
2 answers

From the documentation (I did not save the links in the cited material):

Using application / x-www-form-urlencoded format

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

Browser

In the 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); 

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

Alternatively, you can encode data using the qs library:

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

You can use jQuery.param

 postdata = $.param({param1: 'value1', param2:'value2'}) 

Now you can use postdata, has its own post parameter

0
source

Source: https://habr.com/ru/post/1262260/


All Articles