无法得到symfony2中ajax请求的响应
问题描述:
我是symfony2的新手。基本上我想发送一个变量name
到一个名为sub.html.php
的文件。我想提出一个Ajax请求如下:无法得到symfony2中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;
}
我已经定义的task1
路线如下:
task1:
pattern: /task1/
defaults: {_controller:AcmeTaskBundle:Task:task1}
和TaskController我已经定义task1Action如下:
public function task1Action()
{
return $this->render('AcmeTaskBundle:Default:sub.html.php');
}
但我无法无论如何调用sub.html.php文件。我怎样才能调用这个文件?
答
您可以转发来自TaskController::task1Action()
像这样的请求......
$response = $this->forward('VendorOtherBundleName:ControllerName:ActionName', array(
'some_variable' => $value
));
你可以阅读更多here
+0
当我添加这个东西,然后,我得到的Ajax响应为500 – ucerturohit 2012-08-06 12:58:56
你不“来电” sub.html.php,你犯了一个请求一个URL,它被映射到一个控制器和一个动作。你的'task1'路线是否暴露给客户端? – JamesHalsall 2012-08-06 07:27:43
是的,我已经这样做了。当我像这样调用文件时:http.open(“POST”,“{{path('task1')}}?field =”+ name,false);那么在http.responseText中,它会返回sub.html.php文件的完整代码。如果我想将数据发送给控制器,那么我该怎么做? – ucerturohit 2012-08-06 07:40:20
到什么控制器?它已经通过你的'TaskController' - >'task1Action()' – JamesHalsall 2012-08-06 11:45:19