简体   繁体   English

无法在symfony2中获得ajax请求的响应

[英]unable to get response of ajax request in symfony2

I am new to symfony2. 我是symfony2的新手。 Basically I want to send a variable name to a file named sub.html.php . 基本上,我想将变量name发送到名为sub.html.php的文件。 I am making an ajax request as following : 我正在发出ajax请求,如下所示:

function onsub()
    {
        alert(document.getElementById('source').value);
        var http=new XMLHttpRequest();
        var name="rohit";
        http.open("POST", {{path('task1')}}, false);


        http.onreadystatechange = function() 
        {
        alert(http.status);
            if(http.readyState == 4 && http.status == 200) 
            {

                alert('i m back');
            }
            else
            {
                alert('sorry');

            }
        }  

        http.send();
        return false;
    }

I have defined the route of task1 as follow: 我定义了task1的路由,如下所示:

task1:
    pattern:    /task1/
    defaults:   {_controller:AcmeTaskBundle:Task:task1}

and in TaskController I have defined task1Action as follow: 在TaskController中,我定义了task1Action,如下所示:

public function task1Action()
    {
    return $this->render('AcmeTaskBundle:Default:sub.html.php');
    }

but I am unable to call the sub.html.php file anyhow. 但我还是无法调用sub.html.php文件。 How can I call this file? 我怎么称呼这个文件?

You can forward the request from the TaskController::task1Action() like so... 您可以像这样...从TaskController::task1Action()转发请求。

$response = $this->forward('VendorOtherBundleName:ControllerName:ActionName', array(
    'some_variable'  => $value
));

You can read more here 你可以在这里阅读更多

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM