[英]How to make a POST Ajax request with Symfony and Jquery
我需要在我的symfony項目中存儲一些map參數,為此我需要在我的視圖中實現一些Ajax,它將能夠將一些信息傳遞給控制器。
我閱讀了文檔,嘗試編寫一些代碼,但我無法使其工作。 而Ajax真的很難調試。 這是控制器部分:
/**
* @Route("/ajax", name="_recherche_ajax")
*/
public function ajaxAction()
{
$isAjax = $this->get('Request')->isXMLHttpRequest();
if ($isAjax) {
return new Response('This is ajax response');
}
return new Response('This is not ajax!', 400);
}
和JS:
map.on('zoomend', function(e) {
// use callback e variable
console.log('zoom: ' + e.target.getZoom());
$.ajax({
type: "POST",
url: "/recherche/ajax",
data: {
zoom: e.target.getZoom()
},
dataType: "json",
success: function(response) {
console.log(response);
}
});
});
我檢查它確實存在的url recherche/ajax
並按預期返回'This is not Ajax'。 但是console.log沒有返回任何值......
這是正確的方法嗎?
編輯:看起來控制器無法處理POST請求。 我試圖將注釋修改為:
/**
* @Route("/ajax", name="_recherche_ajax")
* @Method({"GET", "POST"})
*/
但它返回:
([Semantical Error] The annotation "@Method" in method MySite\SiteBundle\Controller\RechercheController::ajaxAction() was never imported. Did you maybe forget to add a "use" statement for this annotation?)
試試這個,
/**
* @Route("/ajax", name="_recherche_ajax")
*/
public function ajaxAction(Request $request)
{
if ($request->isXMLHttpRequest()) {
return new JsonResponse(array('data' => 'this is a json response'));
}
return new Response('This is not ajax!', 400);
}
如果發送Ajax請求,則需要返回json/plaintext/xml
數據,而不是整個Response
對象。
PS:不要忘記為Request
和JsonResponse
添加use statment
編輯:正如您添加的錯誤消息所示,您需要使用以下命令導入注釋@Method
:
use Sensio\\Bundle\\FrameworkExtraBundle\\Configuration\\Method;
我正在尋找整個互聯網,並沒有找到任何類似問題的解決方案。 但我發現它 - >我既沒有控制器問題,也沒有javascript / jquery / ajax問題,也沒有安全問題。 它在...等待它....在HTML中。 我不得不在html標簽中添加type =“button”,否則整個頁面都在刷新。 在調試目的上浪費了4個小時..但是經驗教訓。
如何調試問題? 1.檢查ajax是否在客戶端發送帖子和匹配的帖子路由。 Firefox - > f12 - >網絡 - >觀看POST事件2.檢查symfony profiler(非常有用的工具!) - > /app_dev.php/(開發環境) - >獲取請求/響應子菜單結束最后10個,如果您看到POST路由密切檢查其返回代碼和參數(您不會看到響應,如果其設置不是HTML響應)3。在您的控制器中執行一些操作,如果此路由中的腳本已執行,則可以檢查該操作。 如果是這樣,並且您在服務器端(控制器)或客戶端(twig / ajax / html)看到沒有響應.4。代碼示例:
html中的按鈕(這是我的問題)
<button name="button" id="button" class="button" type="button" value="100"> Click me </button>
在HTML或其他包含的js文件中的Ajax:
function aButtonPressed(){
$.post('{{path('app_tags_sendresponse')}}',
{data1: 'mydata1', data2:'mydata2'},
function(response){
if(response.code === 200 && response.success){
alert('success!');
}
else{
alert('something broken');
}
}, "json");
}
現在..服務器端。 控制器:
namespace AppBundle\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
class JsonApiController extends Controller
/**
* @Route("/api/programmers")
* @Method("POST")
*/
public function sendResponse()
{
if(isset($_POST['data1'])){
$json = json_encode(array('data' => $_POST['data1']), JSON_UNESCAPED_UNICODE);
file_put_contents("test.json", $json);
return new JsonResponse($json);
}
return new Response('didn't set the data1 var.');
}
}
File put contents
在Web目錄中創建新文件。 如果它匹配並且創建了文件意味着,您匹配路線,但沒有得到響應
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.