我有一个问题试图让Symfony和ajax相互配合得很好。我是Symfony和ajax整个世界的新手,所以对我来说很容易。我确信我的代码是一场噩梦,但我正在学习:)
我能够创建一个表单并将我的数据发布到没有Ajax的Symfony(通过URL发布)但是,当我尝试通过ajax(完全相同的形式)执行它时,我得到400的json响应。我知道它与Symfony无法理解进入它的帖子数据有关。在我的控制器操作中,它处理正常,直到它到达“if($ form-> isValid())”部分,这是Symfony不喜欢我的表单的地方。
这是我的jQuery ajax通过表单发送帖子数据:
$(document).ready(function() {
$("#myForm").submit(function(){
// My form
var $form = $(this).closest("#myForm");
// If valid
if($form){
// The url where the form is being posted via ajax
var url = $("#myForm").attr("action");
// Post the data
$.post(url,{
type: "POST",
data: $form.serialize(), // serializing the data being sent
cache: false
},function(data){
// If valid
if(data.responseCode == 200 ){
alert(data.responseCode);
}
else if(data.responseCode==400){
alert(data.responseCode);
}else{
alert("bad response all together...");
}
});
}
return false;
});
});
这是我通过Symfony表单构建器创建的表单:
<form novalidate class="form-horizontal" id="myForm" action="/symfonydev/web/app_dev.php/warehouse/ajax/insert/" method="POST" >
<input type="text" id="warehouse_name" name="warehouse[name]" required="required" placeholder="Location Name" class="input-block-level" value="" />
<input type="text" id="warehouse_address" name="warehouse[address]" required="required" placeholder="Address" class="input-block-level" value="" />
<input type="text" id="warehouse_city" name="warehouse[city]" required="required" placeholder="City" class="input-block-level" value="" />
<input type="text" id="warehouse_state" name="warehouse[state]" required="required" placeholder="State" class="input-block-level" value="" />
<input type="text" id="warehouse_zip" name="warehouse[zip]" required="required" placeholder="Zip" class="input-block-level" value="" />
<input type="text" id="warehouse_email" name="warehouse[email]" required="required" placeholder="Email Address" class="input-block-level" value="" />
<input type="text" id="warehouse_phone" name="warehouse[phone]" required="required" placeholder="Phone" class="input-block-level" value="" />
<input type="text" id="warehouse_fax" name="warehouse[fax]" required="required" placeholder="fax" class="input-block-level" value="" />
<input type="hidden" id="warehouse__token" name="warehouse[_token]" value="5352017c711a3a9d87ca9158334b32ab9f1dd3af" />
<input type="submit" value="Send" />
</form>
也许是因为表单变量的序列化字符串是url编码的,Symfony是不是能够解析这些变量以将它们持久化到db?当ajax通过序列化字符串发布数据时,字段名称的URL编码如下。
warehouse%5Bname%5D=Test&warehouse%5Baddress%5D=Test&warehouse%5Bcity%5D=Test&warehouse%5Bstate%5D=Test&warehouse%5Bzip%5D=Test&warehouse%5Bemail%5D=Test&warehouse%5Bphone%5D=Test&warehouse%5Bfax%5D=Test&warehouse%5B_token%5D=5352017c711a3a9d87ca9158334b32ab9f1dd3af
这是我的控制器动作:
public function ajaxinsertAction(Request $request)
{
// Get user's account
$account = $this->getUser()->getAccount();
// Warehouse form
$warehouse = new Warehouse();
$form = $this->createForm(new WarehouseType(), $warehouse);
if ($request->isMethod('POST')) {
// Get the Account of this user and set it on the warehouse being created.
$account = $this->getUser()->getAccount();
$warehouse->setAccount($account);
$form->bind($request);
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($warehouse);
$em->flush();
$return = array("responseCode"=>200, "response"=>"Valid");
$return = json_encode($return); // json encode the array
return new Response($return,200,array('Content-Type'=>'application/json'));
die;
}else{
$return = array("responseCode"=>400, "response"=>"Invalid");
$return = json_encode($return); // json encode the array
return new Response($return,400,array('Content-Type'=>'application/json'));
die;
}
}
}
感谢您的帮助!
任何人都有关于Symfony和Ajax的好教程吗?我想了解更多关于这个框架的内容,除了Symfony的文档之外似乎只是一些稀疏的信息(虽然它们很少有关于ajax的文档,但它们相当不错)。
干杯!
答案 0 :(得分:3)
对于在漫长的一天之后搜索类似问题的答案并且我被解雇的人。
我处于非常类似的情况。问题是你提交空数据......你提交表单......然后你拉到DOM。
var $form = $(this).closest("#myForm");
应该只是
var form=$(this);
$.post(url, form.serialize(), function(data) {
});
答案 1 :(得分:1)
您是否尝试过手动传递值?
...
$.post(url,{
type: "POST",
"warehouse[city]": $('#warehouse_city').val(),
"warehouse[state]" : $('#warehouse_state').val(),
...
cache: false
}
....