我在试图让 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 是否无法将它们解析出来以将它们保存到数据库中?当 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 的文档)之外,似乎信息很少。
干杯!