jQuery('input').live('click',f开发者_如何学编程unction(e){
$.getJSON(
"/json.php",
function(data){
the_name = data.name;
}
);
});
When we press , it should make a json query.
Bit it gives errors.
In Google Chrome console:
- Failed to load resource http://site.com/json.php
- Uncaught TypeError: Cannot read property 'name' of null
In Firefox console:
- GET http://site.com/json.php - is bolded with red color.
- data is null; the_name = data.name;
The strange is when I open http://site.com/json.php, browser gives me a normal json code like:
{"name":"Mary"}
. It is encoded with php json_encode();
What is the problem?
maybe your json string is not correct:
try
$.get("/json.php", function(data) {alert(data)});
if you see you data in the alert box try:
$.get("/json.php", function(data) {
var obj = $.parseJSON(data);
alert(obj.name)
});
Your json.php
script didn't set the Content-Type: application/json
HTTP header?
Load up Firebug and check the request and response using the Console. Make sure the request is getting sent properly and that the response from the server is properly formatted JSON.
精彩评论