I'm trying to get json code from page.I use this
private function __ajax_admin_search($username = '')
{
$result = $this->admin_login->Admin_Username_Ajax($username);
$count = count($result);
for ($i = 0;$i < $count;$i++)
$arr[$i] = $result[$i]->username;
echo (json_encode($arr));
}
And try to show it by jQuery and JavaScript
$("#AdminSearch").bind("change keyup", function() {
var url = "http://localhost/PmMusic/index.php/admin/ajax/admin_search/"+$("#AdminSearch").val();
$.getJSON(url,function(data){
if (data.length == 0) // or arr == null
{
$("#AutoSearch").hide(1000);
}
else
{
$("#AutoSearch").show(1000);
}
});
});
Now my problem is,I can't detect when __ajax_admin_se开发者_StackOverflow中文版arch doesn't find any result and first if (in JavaScript) doesn't be TRUE(always it's FALSE). how Can I detect in JavaScript when my php code doesn't find any result in database.
Make sure to initialise $arr = Array();
before your for
loop.
Then, the PHP script will return []
as the JSON object, which you can detect in JavaScript without problems.
be aware that you have to set the correct mime type. your javascript is expecting json so you need to set the headers in php. The MIME media type for JSON text is application/json...
header("Content-type: application/json");
精彩评论