Php 向symfony控制器发送json
我需要将json数据传递给我的Symfony控制器。我的ajax函数如下所示:Php 向symfony控制器发送json,php,json,jquery,symfony,Php,Json,Jquery,Symfony,我需要将json数据传递给我的Symfony控制器。我的ajax函数如下所示: var data = '{"firstname":"John"}'; $.ajax({ type: "POST", url: save_url, //path to controller action data: {json:data}, success: function(response) { // Do something
var data = '{"firstname":"John"}';
$.ajax({
type: "POST",
url: save_url, //path to controller action
data: {json:data},
success: function(response) {
// Do something
}
});
在控制器中,我尝试通过以下方式获取数据:
public function createAction(Request $request) {
$data = $this->getRequest()->get('firstname');
return $this->render('MyBundle:Counter:test.html.twig', array(
'data' => $data
));
为了看看这是否有效,我发送了$data
以在模板中进行回音。在Firebug中,我可以看到正在发送的数据,并且一切似乎都正常,但是$data
是空的,没有任何内容得到响应。我哪里做错了
编辑:当我在Fireburg
控制台中检查响应时,我在那里看到了我的数据,但它从未出现在模板中var\u dump($data)
告诉$data
为null
。因此,看起来数据正在发送,但控制器忽略了它。正如马瑞克所注意到的:
$this->getRequest()
已返回请求对象,您正在访问请求的request
属性,但该属性不起作用。或者尝试:
$data = $this->request->get('json');
或使用:
$data = $this->getRequest()->get('json');
当然,您可以将$this->getRequest()
的返回值分配给一个变量,并从那里开始调用该变量的get
方法。。。无论如何,这是我的初步答案,它确实包含了一些更多的提示和注意事项,您可能会发现它们很有用:
您应该能够通过这种方式获取数据,尽管AJAX请求+在模板中回音?这听起来确实有点奇怪。我没有看到您在任何地方将$data
变量传递给$this->render
调用
这是我的一个项目中控制器操作的复制粘贴位。它在那里工作得很好:
public function indexAction()
{
if (!$this->getRequest()->isXmlHttpRequest())
{//check if request is AJAX request, if not redirect
return $this->redirect(
$this->generateUrl('foo_bar_homepage')//changed this, of course
);
}
$id = $this->getRequest()->get('id',false);//works fine
然而,我无法理解你为什么要这样做:
var data = '{"firstname":"John"}';
为什么不干脆去:
$.ajax({
type: "POST",
url: url,//post how you get this URL please...
data: {firstname: 'John'},//jQ will sort this out for you
success: function(response)
{
console.log(response);
}
error: function()
{
console.log('an error occured');
console.log(arguments);//get debugging!
}
});
然后,在控制器中,您可以:
$this->getRequest()->get('firstname');//it should be John
您甚至可以将{json:{firstname:'john'}}
作为数据参数传递给$.ajax
,控制器中唯一的区别是,您必须这样做:
$data = $this->getRequest()->get('json');
$firstName = $data['firstname'];
这应该很好,除非有什么你没告诉我们的:)
概述:这就是我要写的:
public function createAction()
{//no Request param in controller
if (!$this->getRequest()->isXmlHttpRequest())
{//no ajax request, no play...
$this->redirect(
$this->generateUrl('homepage_route')
);
}
$data = $this->getRequest()->get('firstname');
//return json response:
return new Response(json_encode(array('dataReceived' => $data));
//return rendered HTML page:
return $this->render('MyBundle:Counter:test.html.twig', array(
'data' => $data
));
}
当然,那么JS代码应该是:
$.ajax({
type: "POST",
url: 'route/to/create'
data: {firstname:'John'},
success: function(response)
{
console.log(response);
}
});
我已经测试过了,我看不出有什么理由不起作用。它对我来说很好…是您试图检索的内容,既不是参数也不是标题 尝试:
在您的情况下,
$request->request->get('json')
应该可以。请注意,这是@EliasVanOotegem的原始示例,但缺少一些明显的步骤
在控制器中,我读到了一些回复,如“我无法看到这是如何工作的,因为我正在变为null”,这是因为您没有正确设置对象的关键帧
i、 e
正如您现在看到的,访问requerst对象
$request->get('json');
指的是json数据的post键是否应该是$request->get('json')?我在你的例子中尝试了一切,但结果仍然是空的。我更新了我的问题。@tofu:你没有告诉我们你是如何获得数据的。jQ也有很好的默认行为:
$。ajax
以x-www-form-urlencoded mime类型发送数据,因此您无需对其进行解码。即使您将数据作为JSON发送,我也相信Symfony会为您解码。请将您用于解码的代码添加到您的问题中,以便我们可以验证,但主要问题仍然是请求结果为空。我还没有解码,我只是想我可能需要这么做。第一步是确保我可以将数据发送到我的控制器。@tofu:当然可以,但是如果你不告诉我们你是如何将数据输入控制器的(你是如何分配给$data
),我们就不能肯定没有错,对吗?您刚刚粘贴了$data=//获取json并解码
,这并没有显示您如何将任何内容分配到$data
。我需要看看你在那里做什么我删除了,因为我原来的代码是错误的。我已经更新了你的一个建议。
var data = { name : 'john' };
$.ajax({
type: "POST",
url: url,//post how you get this URL please...
data: {json : data},//jQ will sort this out for you
success: function(response)
{
console.log(response);
}
error: function()
{
console.log('an error occured');
console.log(arguments);//get debugging!
}
});
$request->get('json');