ajax post请求处理symfony2控制器中的数据

时间:2022-10-24 12:51:12

i really don't understand how to handle with post data from ajax request. This is my javascript:

我真的不明白如何处理来自ajax请求的帖子数据。这是我的javascript:

$.ajax({
     type: "POST",
     url: Routing.generate('save'),
     contentType: 'application/json; charset=UTF-8',
     data: {
          title: title,                
          description: description,
          questions: questions,              
         }
  });

The only way to get the data inside my controller action is this:

在控制器操作中获取数据的唯一方法是:

$content = $request->getContent()

$content is a url parameter string. Why don't i get the data normally with:

$ content是url参数字符串。为什么我没有正常获取数据:

$request->get('title')

What is the correct way to handle the post data with jquery ajax methd?

使用jquery ajax方法处理post数据的正确方法是什么?

Thank you very much.

非常感谢你。

EDIT

编辑

So, i found out the following issue:

所以,我发现了以下问题:

In my current project the request looks like this:

在我当前的项目中,请求如下所示:

ajax post请求处理symfony2控制器中的数据

$.ajax({
            type: "POST",
            url: Routing.generate('poll_save'),                
            data: {
                title: title                    
            }
        })

The data is requested via Request Payload but i don't know why.

数据是通过Request Payload请求的,但我不知道为什么。

In a clean project the request looks like this:

在一个干净的项目中,请求如下所示:

ajax post请求处理symfony2控制器中的数据

$.ajax({
                type: "POST",
                url: '{{path('_demo')}}',                    
                data: {
                    title: 'title',                
                    description: 'description',
                    questions: 'questions',
                    pollid: 1                        
                }
            })

Anything in my project is going wrong. Do you have an idea why the data is requested via Request Payload?

我项目中的任何内容都出错了。您是否知道为什么通过请求有效负载请求数据?

4 个解决方案

#1


9  

Do you use the request object in your controller?

你在控制器中使用请求对象吗?

<?php
namespace Acme\DemoBundle\Controller;

use Symfony\Component\HttpFoundation\Request;
//...other things to use

class MyController extends Controller
{
    public function handleRequestAction() {

        $request = $this->get('request');
        //request your data
        $title   = $request->get('title');
        //or in one line
        $title   = $this->get('request')->request->get('title');
    }
}
?>

This is my normal way when I want to get data from an ajax call. Could you post what $content contains?

当我想从ajax调用中获取数据时,这是我的正常方式。你能发布$ content包含的内容吗?

I see no problem with posting the data like you did. Constructing a json object might be helpful but the way you're doing it seems fine to me. I did this too.

我发现像你一样发布数据没问题。构造一个json对象可能会有所帮助,但是你这样做的方式对我来说似乎很好。我也是这样做的。

EDIT

编辑

Normally you could also access all data in the request by doing this:

通常,您还可以通过执行以下操作来访问请求中的所有数据:

$all = $request->request->all();

Maybe you could then var_dump() the variables to see if something is in them.

也许你可以然后var_dump()变量,看看是否有东西。

#2


1  

You can construct your json object and pass the JSON object to your controller using JSON.stringify.

您可以使用JSON.stringify构造json对象并将JSON对象传递给控制器​​。

https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/JSON/stringify

https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/JSON/stringify

var obj = {
      title: title,                
      description: description,
      questions: questions              
};

$.ajax({
 type: "POST",
 url: Routing.generate('save'),
 contentType: 'application/json; charset=UTF-8',
 data: JSON.stringify(obj)
});

#3


1  

quiz - form name serialize -populate the variables

测验 - 表格名称序列化 - 填充变量

 $.ajax({
            url: $("#quiz").attr("action"),
            data: $("#quiz").serialize(),
            type: 'POST'
 });

or

要么

$.ajax({
                url: $("#commentForm").attr("action"),
                data: {
                    comment: commentFormID.val()
                },
                type: 'POST'
});

Controller - More like what previous comments suggested.

控制器 - 更像以前的评论建议。

$request = $this->get('request');
$usercomment=$request->request->get('parameterName');

#4


0  

Why Json? I meant is a requirement to content type json? if not, this is the way I handle ajax and using FOSRoutingbundle that I can see you are using.

为何选择Json?我的意思是内容类型json的要求?如果没有,这是我处理ajax和使用FOSRoutingbundle的方式,我可以看到你正在使用。

$(document).ready(function(){
    $('#myForm').submit( function(e){       

        e.preventDefault();
        var $form = $(this);
        var $formPHP = $form.serializeArray();
        var $url = Routing.generate( 'route_to_use');

        $.post( $url, $formPHP, function(data){
        .....
        });

    });    
});

Then in the controller you can use as a normal request.

然后在控制器中,您可以将其用作普通请求。

#1


9  

Do you use the request object in your controller?

你在控制器中使用请求对象吗?

<?php
namespace Acme\DemoBundle\Controller;

use Symfony\Component\HttpFoundation\Request;
//...other things to use

class MyController extends Controller
{
    public function handleRequestAction() {

        $request = $this->get('request');
        //request your data
        $title   = $request->get('title');
        //or in one line
        $title   = $this->get('request')->request->get('title');
    }
}
?>

This is my normal way when I want to get data from an ajax call. Could you post what $content contains?

当我想从ajax调用中获取数据时,这是我的正常方式。你能发布$ content包含的内容吗?

I see no problem with posting the data like you did. Constructing a json object might be helpful but the way you're doing it seems fine to me. I did this too.

我发现像你一样发布数据没问题。构造一个json对象可能会有所帮助,但是你这样做的方式对我来说似乎很好。我也是这样做的。

EDIT

编辑

Normally you could also access all data in the request by doing this:

通常,您还可以通过执行以下操作来访问请求中的所有数据:

$all = $request->request->all();

Maybe you could then var_dump() the variables to see if something is in them.

也许你可以然后var_dump()变量,看看是否有东西。

#2


1  

You can construct your json object and pass the JSON object to your controller using JSON.stringify.

您可以使用JSON.stringify构造json对象并将JSON对象传递给控制器​​。

https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/JSON/stringify

https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/JSON/stringify

var obj = {
      title: title,                
      description: description,
      questions: questions              
};

$.ajax({
 type: "POST",
 url: Routing.generate('save'),
 contentType: 'application/json; charset=UTF-8',
 data: JSON.stringify(obj)
});

#3


1  

quiz - form name serialize -populate the variables

测验 - 表格名称序列化 - 填充变量

 $.ajax({
            url: $("#quiz").attr("action"),
            data: $("#quiz").serialize(),
            type: 'POST'
 });

or

要么

$.ajax({
                url: $("#commentForm").attr("action"),
                data: {
                    comment: commentFormID.val()
                },
                type: 'POST'
});

Controller - More like what previous comments suggested.

控制器 - 更像以前的评论建议。

$request = $this->get('request');
$usercomment=$request->request->get('parameterName');

#4


0  

Why Json? I meant is a requirement to content type json? if not, this is the way I handle ajax and using FOSRoutingbundle that I can see you are using.

为何选择Json?我的意思是内容类型json的要求?如果没有,这是我处理ajax和使用FOSRoutingbundle的方式,我可以看到你正在使用。

$(document).ready(function(){
    $('#myForm').submit( function(e){       

        e.preventDefault();
        var $form = $(this);
        var $formPHP = $form.serializeArray();
        var $url = Routing.generate( 'route_to_use');

        $.post( $url, $formPHP, function(data){
        .....
        });

    });    
});

Then in the controller you can use as a normal request.

然后在控制器中,您可以将其用作普通请求。