嗨,我正在尝试通过axios从symfony后端访问vue.js数据数组。这是我的密码。
var vm = new Vue({
el: '#el',
delimiters: ["[[", "]]"],
data: {
brand: 0,
model: 0,
country: "europe",
},
created: function () {
},
updated: function () {
axios.post('http://localhost:81/lnt/public/member/car_result', {data: this.data})
.then(function (response) {
});
}
});这是我的符号代码,
/**
* @Route("/car_result", name="car_result", methods="POST")
*/
public function carResult(Request $request) {
$data = $request->getContent();
$data = json_decode($data, true);
$brand = $data['brand'];
.......
}但不幸的是,我得到的是没有所谓的品牌指数。如果有人能在这件事上帮我,那就太好了。认为我需要找到一种方法将完整的数据数组发送到后端,我可以这样发送数据,
axios.post('http://localhost:81/lnt/public/member/car_result', {brand: this.brand})发布于 2019-01-24 19:14:35
这个代码有两个问题。
1 this.data未定义
您的数据如下:
{data: this.data}你可能希望有一个结构
{
data: {
brand: 0,
model: 0,
country: "europe",
}
}但是由于this.data不是Vue实例的数据属性,所以是不工作的。它看起来很神奇,但是调用this['property']会询问Vue对data的反应性属性的值,所以如果您想获得brand,就应该输入this.brand。
2您的数据模型在客户机和服务器上是不同的。
如果要实现json的这个结构,那么在Symfony的控制器中执行以下操作
$brand = json_decode($request->getContent(), true)['brand'];但实际上,你的要求在第一个层次上没有“品牌”的关键。您应该将后端代码替换为
$brand = json_decode($request->getContent(), true)['data']['brand'];或者将客户端代码粘贴到请求正文中,如
{brand: this.brand}https://stackoverflow.com/questions/53723224
复制相似问题