简体   繁体   中英

get JSON doesnt work for me

I made php file that parse JSON object. here's an example of result from the php:

{"ID":"951","NameOfEvent":"\u05dc\u05d9\u05d9\u05df \u05d4\u05e7\u05e8\u05d9\u05d5\u05e7\u05d9 \u05d1\u05e9\u05dc\u05d9\u05e9\u05d9","Plan":"\u05d4\u05d2\u05d5\u05e8\u05df - \u05dc\u05d9\u05d9\u05df \u05d4\u05e7\u05e8\u05d9\u05d5\u05e7\u05d9 \u05d1\u05e9\u05dc\u05d9\u05e9\u05d9 @ \u05d4\u05d2\u05d5\u05e8\u05df.\r\n\r\n\u05de\u05d5\u05e2\u05d3\u05d5\u05df \u05d4\u05d2\u05d5\u05e8\u05df \u05e9\u05d5\u05db\u05df \u05d1\u05e7\u05d9\u05d1\u05d5\u05e5 \u05de\u05e8\u05d7\u05d1\u05d9\u05d4 \u05e9\u05d1\u05e2\u05de\u05e7 \u05d9\u05d6\u05e8\u05e2\u05d0\u05dc.","Avg":"0","Views":"23"}

I tried to get the data using the following jquery code:

function gettopevents()
{
$.getJSON("jsontopevents.php", function(json) {
    alert(json.NameOfEvent);
});
}

I call the function by using button with onclick event.

I dont get whats wrong with my function... I am using the same function to read data from facebook graph and it works..

edit: Wierd thing - it works in IE but not in FF and Chrome

Thanks.

What you've quoted works: http://jsbin.com/iwumu4 Tried it in IE6, IE7, IE8, Chrome 9, Firefox 3.6, Safari 5, and Opera 11 on Windows; and Chrome 9, Firefox 3.6, and Opera 11 on Linux.

This suggests the problem lies elsewhere in the code. Your best bet is to use a debugger. Chrome, Safari, Opera, and IE8 all have built-in debuggers. You can get Firebug for Firefox, and VS.Net will debug JavaScript on IE6 and IE7. In the debugger, look at the console as a first thing, to see if any errors are reported. If there aren't, set a breakpoint on the $.getJSON call and also the alert , and examine the data.

Update : (After your note below.) Ah, sounds like a caching problem. I should have thought of that. Since getJSON uses GET , caching is definitely something you have to watch for.

getJSON is just a wrapper for ajax , so you could use

$.ajax({
    url:      "jsontopevents.php",
    cache:    false,
    dataType: 'json',
    success:  function(json) {
        alert(json.NameOfEvent);
    }
});

Note the cache argument. See the ajax docs for details.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM