jQuery ajax post请求处理symfony2控制器中的数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16253131/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ajax post request handle data in symfony2 controller
提问by Sandor Farkas
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 方法处理帖子数据的正确方法是什么?
Thank you very much.
非常感谢。
EDIT
编辑
So, i found out the following issue:
所以,我发现了以下问题:
In my current project the request looks like this:
在我当前的项目中,请求如下所示:
$.ajax({
type: "POST",
url: Routing.generate('poll_save'),
data: {
title: title
}
})
The data is requested via Request Payloadbut i don't know why.
数据是通过请求有效负载请求的,但我不知道为什么。
In a clean project the request looks like this:
在一个干净的项目中,请求如下所示:
$.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?
我的项目中的任何事情都出错了。您知道为什么通过Request Payload请求数据吗?
回答by SirDerpington
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()
变量来看看它们中是否有东西。
回答by Teffi
quiz - form name serialize -populate the variables
quiz - 表单名称序列化 - 填充变量
$.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');
回答by anazimok
You can construct your json object and pass the JSON object to your controller using JSON.stringify.
您可以构建 json 对象并使用 JSON.stringify 将 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)
});
回答by Richard Pérez
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 的要求?如果没有,这就是我处理 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.
然后在控制器中,您可以将其用作普通请求。