开发者

Not able to call .php file through JQuery

Below is my JQuery code. I just added an 'alert' to test whether data is retrieved correctly or not. But this code is not working. I think it is simply not able to get inside the file "filldistricts.php".

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.js" />

<script type="text/javascript">
$(document).ready(function(){
    $("#inDistrict").change(function(){
    $.getJSON("filldistricts.php",function(j){
        alert(j[0]);
    })
  })
})
</script>

Below I am giving the code of "filldistricts.php":

<?php
    $query = "SELECT districtid FROM districtmaster";
    try
    {
        $result = mysql_query(开发者_运维知识库$query);
        $c = "";

        while($row = mysql_fetch_assoc($result))
        {
            $c = $c.$row['districtid'].",";
        }
        $c = $c."Select";
        echo json_encode(array($c));
    }
    catch(exception $ex)
    {
        echo "<script type='text/javascript'>alert('".$ex."');</script>";
    }
?>

Where is the problem?


Maybe php is returning an exception? I thinks that doens't work with getJSON because it's not JSON..


Since you're using getJSON in jQuery, the server return type should be JSON string, thus you cannot put javascript and alert() when there's an error.

Thus instead of

echo "<script type='text/javascript'>alert('".$ex."');</script>";

use

echo json_encode(array($ex)) ;

that will allow you to see the exception properly.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜