jQuery 中的自动完成功能刚刚...停止工作了?
我让 jQuery 自动完成与 CodeIngiter 完美配合,但莫名其妙地它完全停止运行了。当我访问自动完成控制器时,我仍然看到正确的数组 - Javascript 只是不返回 JSON 数据。奇怪的是,它运行得很好,然后突然停止工作了。
这是我的 Javascript:
$( "#clubs-complete" ).autocomplete({
source: function(request, response) {
$.ajax({
url: 'http://www.myurl.com/create/autocomplete',
data: 'term='+$("#clubs-complete").val(),
dataType: "json",
type: "POST",
success: function(data){
alert(data);
response(data);
}
});
},
minLength: 1
});
这是我的控制器:
public function autocomplete()
{
// Search term from jQuery
$term = $this->input->post('term');
$this->db->select('name','address2');
$this->db->from('clubs');
$this->db->like('name', $term);
$suggestions = $this->db->get()->result();
if (count($suggestions) > 0) {
$data = array();
foreach ($suggestions as $suggestion) {
array_push($data, $suggestion->name);
}
// Return data
echo json_encode($data);
}
}
有人知道发生了什么吗? javascript 函数中的警报现在和过去一样不返回任何内容。当我直接访问 URL 时,我仍然看到完整的数组。
请帮忙,我的头发都快掉光了。
I had jQuery autocomplete working perfectly with CodeIngiter when inexplicably it just stopped functioning completely. When I visit the controller for the autocomplete I still see the correct array - Javascript just isn't returning the JSON data. What makes this weird is that it was working fine, and then out of the blue just stopped working.
Here's my Javascript:
$( "#clubs-complete" ).autocomplete({
source: function(request, response) {
$.ajax({
url: 'http://www.myurl.com/create/autocomplete',
data: 'term='+$("#clubs-complete").val(),
dataType: "json",
type: "POST",
success: function(data){
alert(data);
response(data);
}
});
},
minLength: 1
});
Here's my controller:
public function autocomplete()
{
// Search term from jQuery
$term = $this->input->post('term');
$this->db->select('name','address2');
$this->db->from('clubs');
$this->db->like('name', $term);
$suggestions = $this->db->get()->result();
if (count($suggestions) > 0) {
$data = array();
foreach ($suggestions as $suggestion) {
array_push($data, $suggestion->name);
}
// Return data
echo json_encode($data);
}
}
Does anyone have any idea what's going on? The alert in the javascript function returns nothing now, and it used to. When I visit the URL directly I still see the full array.
Please help, I'm tearing my hair out.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 IE 中,有开发人员工具可用,如果您按 F12。 Firefox 中有类似的东西,称为 Firebug。在其中任何一个中,您都可以调试浏览器内的 javascript。在源 fn 和 success 函数中设置断点,它可能会给您一些见解。
您可能还需要一个 http 调试代理,例如 Fiddler2 或 Charles,它可以让您看到传出的 HTTP请求及其相应的响应。 Fiddler2 在 Windows 上运行,可与 FF 和 IE 以及几乎所有其他 http 客户端配合使用。这将使您看到 AJAX 服务返回到浏览器内 javascript 的消息。
这些事情应该让您深入了解“不工作”的问题。
In IE there are developer tools available if you press F12. There's something similar in Firefox called Firebug. In either of these you can debug in-browser javascript. Set breakpoints inside the source fn and also within the success function, it may give you some insight.
You also may want to get an http debugging proxy, something like Fiddler2 or Charles, which will let you see outgoing HTTP requests and their corresponding responses. Fiddler2 runs on Windows and works with FF and IE, and pretty much every other http client. This will let you see the messages that your AJAX service is returning to the in-browser javascript.
Those things ought to give you insight into the "not working" problem.