开发者

.get() in jquery

I asked a question with the above title a bit ago titled "gettting .get() in jquery to work" and I had made a silly mistake in the code which everyone jumped on. I fixed that but the .get() doesn't seem to be working. Its supposed to print the name John and 2pm on the bottom of the body and it doesn't. My path names are correct. Can someone help?

<html>
<head>
<title>Testing Site</title>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
        $("#id").click(function(){  
            $('#hello').css('background-color', 'blue');
            $.get("test.php",
                function(data){
                    $('body').append( "Name: " + data.name )    //John
                    .append( "Time: " + data.time );            //2pm
                    $('body').css('background-color',     'red');   
                }, "json");
        });
});
</script>

</head>
<body>
<span id="hello">Hello World!</span>
<input type="button" id="id" value="Click Here"></input>
</body>
</html>

heres the php

<?php echo js开发者_开发技巧on_encode(array("name"=>"John","time"=>"2pm")); 
?>


you may have better luck using the jQuery .getJSON() function, or parsing the data as JSON before using it like you are currently.

jQuery getJSON


What version of jQuery are you using? I can reproduce your issue if I go all the way back to 1.3.2. Starting from 1.4, things seem to work as expected. You can see this if you console.log your data variable. With 1.3.2, you'll see a string as the output, with 1.4+, you'll see an object.


As GSto suggests, using $.getJSON will work, even with version 1.3.2.


You're telling it to expect json. Are you sending json? Try adding

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

to the top of your PHP file.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜