AngularJS向Symfony控制器发布POST Ajax请求 [英] POST Ajax request by AngularJS to Symfony controller

查看:43
本文介绍了AngularJS向Symfony控制器发布POST Ajax请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从我的angularJs控制器向我的Symfony控制器发出Ajax请求.但是,由于未知原因,我无法在Symfony控制器中接收数据.我的控制器被调用,我可以返回一些我将在AngularJS端的成功函数中看到的信息.但是,我无法通过Symfony控制器检索通过AngularJs发送的数据.

这是我在AngularJS方面所做的事情:

  $ http.post('{{path('admin_ima_processmanagement_project_save',{'id':object.id})}}',{"projectJson":"test"}).成功(功能(数据,状态,标题,配置){console.log("yeah");console.log(data);}).错误(函数(数据(状态,标题,配置){//如果发生错误,则异步调用//或服务器返回错误状态的响应.console.log("oh non");console.log(data);}); 

我可以在执行此请求后在控制台中看到是".

在我的Symfony控制器中,我具有以下内容:

  $ request = $ this->容器-> get('request');$ projectJson = $ request-> query-> get('projectJson');$ response = array("code" => 100,"success" => true,"projectJson" => $ projectJson);返回新的Response(json_encode($ response)); 

在控制台上,调用后,我得到 {"code":100,"success":true,"projectJson":{}} 表示不幸的是projectJson为空...

我应该怎么做才能检索从客户端发送的数据?&

解决方案

在类 Request 中,属性 query 引用 GET 参数.

在您的情况下,您需要访问 POST 属性中的 POST 参数.

因此您的代码应如下所示:

  $ projectJson = $ request-> request-> get('projectJson'); 

有关 Request 的更多信息,您会找到此处.

I'm trying to do an Ajax request from my angularJs controller to my Symfony controller. However, for an unknown reason, I cannot receive the data in my Symfony controller. My controller gets called and I can return some information that I will see in the success function on the AngularJS side. However, the data I'm sending via AngularJs cannot be retrieved on the Symfony controller.

Here's what I'm doing on the AngularJS side:

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

I can see in my console "yeah" that is appearing after the execution of this request.

In my Symfony controller, I have the following:

$request = $this->container->get('request');

$projectJson = $request->query->get('projectJson');

$response = array("code" => 100, "success" => true, "projectJson" => $projectJson);

return new Response(json_encode($response));

On the console, after the call, I get {"code":100,"success":true,"projectJson":{}} meaning that projectJson is unfortunately empty...

What should I do to retrieve the data that I'm sending from my client ?&

解决方案

In class Request property query refers to GET parameters.

In your case you need to access to POST parameters, which are in request property.

So your code should look like this:

$projectJson = $request->request->get('projectJson');

More info about Request you will find here.

这篇关于AngularJS向Symfony控制器发布POST Ajax请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆