开发者

ajax() method in jquery returns null

I'm trying to m开发者_StackOverflowake a very simple query:

$.ajax({
        url: "ajaxfunc.php",
        dataType: 'json',
        success: function(data){
        alert(data.resp);
    }
});

And in ajaxfunc.php I have

<?php echo json_encode(array("resp"=>"1")); ?>

But for a strange reason data is always null and data.resp is undefined.

Where can be the problem?


You forgot to set the content type in PHP.

header('Content-Type: application/json');

When you set dataType: 'json' in jQuery, it atually expects JSON content from the server.


i got this working in my system.

<script language="javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.js"></script>
<script language="javascript">
$(document).ready(function(){

$.ajax({
    url: "b.php",
    async:false,
    dataType: 'json',
    success: function(data){
    alert(data.resp);
}
});

                       });
</script>

b.php

<?php 

$k['resp'] = 66;
echo json_encode($k);

?>


You can try this:

<?php

$data->resp = 1;
echo json_encode($data);

?>
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜