我正在尝试使用Ajax
从数据库重新加载数据。但是,Ajax
不会调用我在 url:
中指定的 Controller 操作。这是我的 Ajax
代码:
function selectFieldChanged(id){
$.ajax({
type: "POST",
url: Routing.generate('demo_ajax'),
data: id,
success: function(){
alert("Success");
},
error: function(XMLHttpRequest, textStatus, errorThrown)
{
alert('Error : ' + errorThrown);
}
});
}
$(document).ready(function(){
var id = $(this).val();
$('#form_patient').change(function(){selectFieldChanged(id)});
});
routing.xml
:
demo_ajax:
pattern: /ajax/patient
defaults: { _controller: DemoBundle:Default:index}
options:
expose: true
因此,我尝试简单地在 indexAction
中回显该值,以查看它是否被调用。
public function indexAction(Request $request)
{
if($request->isXmlHttpRequest()){
echo "xmlHttpRequest is called";
}
if($request->getMethod()=='POST'){
echo 'POST is called';
}
}
但是,我没有从 indexAction
中得到任何信息,但我从我的 Ajax
中收到了警报消息 `Success
,做了什么我做错了吗?
最佳答案
成功回调从您的服务器接收数据,因此必须声明一个变量来捕获它:
success: function(data){ // <-----
console.log(data);
alert("Success");
},
关于javascript - 如何使用 Ajax 调用特定 Controller 操作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21832631/