我正在尝试从我的angularJs控制器向我的Symfony控制器发出Ajax请求。但是,由于未知原因,我无法在Symfony控制器中接收数据。我的控制器被调用,我可以返回一些我将在AngularJS侧的success函数中看到的信息。但是,我通过AngularJs发送的数据无法在Symfony控制器上检索。
以下是我在AngularJS方面所做的事情:
$http.post('{{ path('admin_ima_processmanagement_project_save', {'id':object.id}) }}',{"projectJson":"test"}).
success(function(data, status, headers, config) {
console.log("yeah");
console.log(data);
}).
error(function(data, status, headers, config) {
// called asynchronously if an error occurs
// or server returns response with an error status.
console.log("oh non");
console.log(data);
});
我可以在我的控制台中看到“是的”,这是在执行此请求后出现的。
在我的Symfony控制器中,我有以下内容:
$request = $this->container->get('request');
$projectJson = $request->query->get('projectJson');
$response = array("code" => 100, "success" => true, "projectJson" => $projectJson);
return new Response(json_encode($response));
在控制台上,在调用之后,我得到{"code":100,"success":true,"projectJson":{}}
意味着projectJson很遗憾是空的......
如何检索我从客户端发送的数据?&
答案 0 :(得分:1)
在课程Request
中,属性query
引用GET
个参数。
在您的情况下,您需要访问POST
属性中的request
个参数。
所以你的代码应该是这样的:
$projectJson = $request->request->get('projectJson');
有关Request
的更多信息,您会找到here。
答案 1 :(得分:1)
Symfony2不支持AngularJS $ http数据。因为AngularJS将数据作为请求体发送,而SF2只读取$ _GET和$ _POST。
你有2个解决方案: