如何使用Symfony和Jquery发出POST Ajax请求 [英] How to make a POST Ajax request with Symfony and Jquery

查看:202
本文介绍了如何使用Symfony和Jquery发出POST Ajax请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在我的symfony项目中存储一些map参数,为此我需要在我的视图中实现一些Ajax,它能够将一些信息传递给控制器​​。

I need to store some map parameter in my symfony project, to do this i need to implement some Ajax in my view which will be able to pass some info to the controller.

我阅读了文档,尝试编写一些代码,但我无法使其工作。而Ajax真的很难调试。
这是控制器部分:

I read the docs, try to write some code but i can't make it works. And Ajax is really painfull to debug. Here is the controller part :

 /**                                                                                   
 * @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);
        }
    });

});

我检查网址 recherche / ajax 它确实存在并按预期返回'This is not Ajax'。但是console.log没有返回任何值......

I check the url recherche/ajax it does exist and return the 'This is not Ajax' as expected. But the console.log does not return any value...

这是正确的方法吗?

编辑:
看起来控制器无法处理POST请求。我尝试将注释修改为:

EDIT : It looks like the controller can't handle POST Request. I tried to modify the annotations to :

 /**                                                                                   
 * @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 数据,而不是整个响应对象。

In case of you sending an Ajax request, you need to return json/plaintext/xml data, and not a whole Response object.

PS:不要忘记为请求 JsonResponse 添加使用陈述

PS: Do not forget to add use statment for Request and JsonResponse

编辑:正如您添加的错误消息所示,您需要导入注释 @Method 通过使用:

EDIT : As the error message you added says, you need to import the annotation @Method by using :

使用Sensio \ Bundle \FrameworkExtraBundle \Configuration\Method;

这篇关于如何使用Symfony和Jquery发出POST Ajax请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆