Axios posting params not read by $_POST

你说的曾经没有我的故事 提交于 2019-11-26 09:48:20

问题


So I have this code:

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

Originally I had the normal axios.post but I changed to this because I thought it might have been a header problem. However I am still detecting nothing in my $_REQUEST nor $_POST. However, it is receiving data in file_get_contents(\"php://input\").

Any idea what is wrong?

Edit

Okay I think I know what\'s wrong. It\'s posting it as a json object so it can only be read in the php://input. How do I change it to a normal string in axios?


回答1:


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 }));



回答2:


var params = {
    data1: 'string',
}

axios.post(url, params).then(function(response) {
    //code here 
});

or

axios.post(url, {data1: 'string' }).then(function(response) {
    //code here 
});

api

$_POST = json_decode(file_get_contents("php://input"),true);
echo $_POST['data1'];



回答3:


You can use jQuery.param

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

You can now use postdata has your post parameter




回答4:


Just wanted to share my insights, I was facing a similar problem and solved it by the following code set

JS

const instructions_str = {
  login: {
    "type": "devTool",
    "method": "devTool_login",
    "data": {
        "username": "test",
        "password": "Test@the9" 
    }
  },
  tables: {
    "type": "devTool",
    "method": "devTool_get_all_tables",
    "data": ""
  }
};

const body = {
    firstName: 'Fred',
    lastName: 'Flintstone',
    name: "John",
    time: "2pm",
    instructions : JSON.stringify(instructions_str)  
};

function decodeData(data) {
  const serializedData = []

  for (const k in data) {
    if (data[k]) {
      serializedData.push(`${k}=${encodeURIComponent(data[k])}`)
    }
  }

  return serializedData.join('&')
};

const body2 = decodeData(body);

axios.post('URL', body2)
  .then(response => {
    console.log("contextApi got it", response);
  }).catch(error => {
      console.log("contextApi error.response", error.response);
  });

PHP

// set content return type
header('Content-Type: application/json');

// Setting up some server access controls to allow people to get information
header("Access-Control-Allow-Origin: *");
header('Access-Control-Allow-Methods:  POST, GET');

// This way I can check and see what I sent
$postVars_array = $_POST ?? parse_str(file_get_contents("php://input"),$postVars_array) ?? [];
echo json_encode($postVars_array);

I also found this github page very helpful https://github.com/axios/axios/issues/1195



来源:https://stackoverflow.com/questions/41457181/axios-posting-params-not-read-by-post

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!