[英]Not working : Send POST Data with Ajax to a Symfony2 Controller
我想將發布數據發送到Symfony控制器,但是它不起作用。 當我使用AJAX發送數據時,它正在發送POST數據,但在控制台中顯示紅色鏈接,沒有錯誤消息或狀態。 但凈FF為200ok。 它以正常形式成功發送了請求。
這是我的Javascript代碼:
function addprivate() {
var form_data = $('#private_tuition').serialize();
var getTeamsUrl = Routing.generate('addprivatetuition', {
id: form_data
});
$.ajax({
type: "post",
url: getTeamsUrl,
data: form_data,
success: function(response) {
if (response) {
} else {
}
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
$.notify('Error : Record not found !!', {
"status": "danger",
"pos": "top-center"
});
}
});
}
以下是表單標簽和js函數:
<form method="post" role="form" id='private_tuition' action="{{base_url}}/privatetutionpdf/" >
<button type="submit" class='btn btn-primary' onclick='addprivate()' name="btn-save">
<strong>Generate PDF for client</strong>
</button>
這是接收數據的PHP Symfony Controller方法:
namespace Suntec\Marcus\AssignmentBundle\Controller;
use Symfony\Component\HttpKernel\Bundle\Bundle;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Core\SecurityContextInterface;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
class DefaultController extends Controller
{
....
/*****************************************************************/
/**
* @Route("/addprivatetuition", name="addprivatetuition", options={"expose"=true})
* @Template()
*/
public function addprivatetuitionAction(){
return array();
//return array();
}
...
}
看來您必須阻止表單的默認操作,因為當您單擊提交按鈕時,將提交html表單。
只通過javascript做某事。 喜歡:
$('#private_tuition').submit(function(e){
e.preventDefault();
}
要么
$('#private_tuition').submit(function(){
return false;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.