[英]How to call php controller action from javascript to get json data?
I'm trying to call a controller action from javascript to get data in json format. 我正在尝试从javascript调用控制器操作来获取json格式的数据。 I guess it should be pretty clear using
$.getJSON
function. 我想使用
$.getJSON
函数应该很清楚。 But it doesn't work in my code. 但它在我的代码中不起作用。 Is there anyone who can explain me what is wrong?
有没有人可以解释我有什么问题?
IndexController action IndexController动作
public function jsonAction()
{
$this->_disableView();
$data[0]["name"] = "test";
$data[0]["lat"] = 50.080746;
$data[0]["long"] = 14.426373;
echo json_encode($data);
}
When I test it on url domain/index/json I see json as expected. 当我在url domain / index / json上测试它时,我看到json如预期的那样。 Now the relevant script in document ready function:
现在文档就绪函数中的相关脚本:
$.getJSON("index/json",function(data){
alert("hello");
});
But no alert is shown. 但没有显示警报。 In the console I can see
在控制台我可以看到
Uncaught TypeError: jQuery(...).asmSelect is not a function
GOT IT! 得到它了! I was missing slash at the behinning
$.getJSON("/index/json",function(data){});
我在
$.getJSON("/index/json",function(data){});
中缺少斜线$.getJSON("/index/json",function(data){});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.