简体   繁体   English

通过axios传递呼叫传递参数

[英]Passing params through axios post call

I'm currently using vue-resource to perform some ajax call in my webpack project. 我目前正在使用vue-resource在我的webpack项目中执行一些ajax调用。 As soon as I make "get" calls all works perfectly but when I try a post one my PHP does not get any parameter and if I try to do var_dump($_POST) the result is an empty array. 一旦我执行“ get”调用,所有功能都可以正常工作,但是当我尝试发布一个帖子时,我的PHP没有任何参数,如果我尝试执行var_dump($ _ POST),结果将为空数组。 My code is quite basic : 我的代码很基本:

let data = {req: 'req_tipe', ch: 001 };
this.$http.post('compute.php', data).then(response => {
    //do stuff with response if ok             
    },
    response => {
    //do stuff about error
    });

I can't figure out what I'm missing 我不知道我在想什么

UPDATE: I did the same with axios and vue-axios but my issue still remain: no post parameters 更新:我对axios和vue-axios进行了相同的操作,但是我的问题仍然存在:没有发布参数

let data = {req: 'req_tipe', ch: 001 };
this.axios.post('compute.php', data).then(response => {
    //do stuff with response if ok             
    },
    response => {
    //do stuff about error
    });

Maybe you can change like following: 也许您可以进行如下更改:

let data = new FormData();
data.append('req','req tipe');
data.append('ch','001');
this.axios.post('compute.php', data).then(response => {
//do stuff with response if ok             
},
response => {
//do stuff about error
})

have a try, or see issues 318 for more information. 尝试一下,或查看问题318以获取更多信息。

The problem is, that you do a POST request with Content-Type "application/json" which is the default in Axios when you send an object as data. 问题是,您使用内容类型“ application / json”执行POST请求,这是将对象作为数据发送时Axios中的默认设置。

PHPS $_POST only works when the request is sent with application/x-www-form-urlencoded as the content type and the data as a Querystring. PHPS $ _POST仅在使用application / x-www-form-urlencoded作为内容类型并将数据作为Querystring发送请求时起作用。

When using a library like qs, the following should work: 当使用像qs这样的库时,以下应该工作:

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

axios automatically uses the right content type when the input data is a querystring. 当输入数据是查询字符串时,axios自动使用正确的内容类型。

You also can use the json data, but then you have to change the PHP backend into something like this: 您还可以使用json数据,但随后必须将PHP后端更改为如下所示:

$data = json_decode(file_get_contents('php://input'), true);
print_r($data);

Some frameworks like Symphony can handle this automatically. 诸如Symphony之类的某些框架可以自动处理此问题。

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

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