8

I'm struggling to get this script working. It's basically a simple ajax call to retrieve data from a php which returns a JSON code.

function refreshWindows(){
if(AjaxPull && AjaxPull.readystate != 4){
    AjaxPull.abort();
}
AjaxPull = $.ajax({
    type: 'POST',
    url: $path,
    data: {
        ajax: true,
        mode: 'update',
        to: Math.round(currentDate.getTime() / 1000),
        from: Math.round(previousDate.getTime() / 1000)
    },
    dataType: "json",
    success: function (data) {
        alert(data); //that's for debug
        $replies = data.Updates;
        $.each($replies ,function(group,value) {
            if (value!=''){
                $("#group"+group+" .content").append(value);
                $("#group"+group+" .content").stop().animate({ scrollTop: $("#group"+group+" .content")[0].scrollHeight }, 800);
                if (!$("#group"+group+" .Window").is(':visible')) {
                    $("#group"+group+" .BottomBox").fadeTo('fast', 0.5).fadeTo('fast', 1.0);
                }
            }
        });
        previousDate = currentDate;
        currentDate = new Date();
        timeController.push( setTimeout(function(){refreshChatWindows();}, 500) );
    }
});

}

The error I get in Internet Explorer is:

SCRIPT5007: Unable to get value of the property 'Updates': object is null or undefined

Everything works fine in Firefox and Google Chrome.

Initially my code was made using .get but someone suggested switching to the .ajax - well, it didn't help. I tried using .done(function(data){ but it didn't work either. I also tried sending all of the data in my URL opposite to the data property, it worked fine in FF, but IE still popped the same error. Finally I tried adding different headers to the PHP, like for example header('Content-Type: application/json'); but it didn't change anything. I run out of ideas / possible solutions I could fine on stackoverflow, so any help would be appreciated.

In IE I went to Developer Tools, network tab and tried to see if everything works - yes, the request is being sent correctly with all the data, and a response I receive is correct JSON, just as it is in Firefox:

{"Updates":{"1":"","2":"","3":"","5":"","6":"","7":"","8":""},"time":{"from":"1367489761","to":"1367489761"}}

which gets me really confused, cause I'd have thought that Undefined error might happen only because something is not being sent back in IE for whatever reason, but clearly: It's not the case. I get my JSON back. Only IE for some unknown reason still thinks that data is undefined.

MarcinWolny
  • 1,384
  • 2
  • 23
  • 38
  • It would help if you specified which IE version(s) you're testing with. – Spudley May 09 '13 at 14:28
  • you've got an `alert()` in there; what does it show you? (and by the way, it would be better to use `console.log()`). – Spudley May 09 '13 at 15:21
  • console.log errors in IE8, so: no, it's not better. Especially not when I debug IEs. Alert shows "undefined". – MarcinWolny May 10 '13 at 08:46
  • 1
    console.log *is* supported in IE8 (you have to have the dev tools open when it's called though). But never mind; if the alert shows 'undefined', then that answers the question I was trying to ask, which is what data comes into the success function. – Spudley May 10 '13 at 09:19
  • `application/json` is the right content type, but just to make sure of things, have you tried other content types? eg [this answer](http://stackoverflow.com/a/10354432/352765) states that jQuery recommends using text/html. – Spudley May 10 '13 at 09:23
  • First I tried to send data with no content-type header at all. Than I found somewhere on stackoverflow that headers might cause errors, so I tried suggested `text/html`, `application/js` and `application/json`. None of them worked. – MarcinWolny May 10 '13 at 10:24
  • Could you post the actual output of the PHP page - or preferably provide a URL. It's possible the console is changing the output so it looks correct. I'm not sure about IE9/10, but older versions of IE would fail if you had an unnecessary comma at the end of a list. – Craig Buckler May 11 '13 at 07:14
  • Exact php output: `{"chatUpdates":{"1":"","2":"","3":"","5":"","6":"","7":"","8":"","9":""},"time":{"from":"1368442919","to":"1368442919"}}` – MarcinWolny May 13 '13 at 11:04
  • might be stupid suggestion but with IE you have to take it all. try `updates` with small letter and check for case sensitivity. – mamdouh alramadan May 15 '13 at 22:30

3 Answers3

4

Ok, I found a solution finally. Basically:

  • Remove any headers sent by PHP script. Especially: Content-type headers. (luckily - seems like sessions still can be used)
  • Use }).done(function ( data ) { instead of success: function (data) {

and that's all. Suddenly it started to work. It's very weird. Seems like the shotgun tactic (randomly changing bits of code till it works) is actually a valid way of solving problems. hehehe

MarcinWolny
  • 1,384
  • 2
  • 23
  • 38
0

I have a similar json call that returns data that looks like this:

{"GetTombstoneDataRestResult":{"AlphaSort":"Arai Junichi","Classification":"A&D Design Object"...etc

In other words, a lot like your json data. To reference it in jQuery, I use the callback name.

     $.ajax({
     type: "GET",
     dataType: "jsonp",
     url: url,
     success: function (result) {

     $('#screenshot').append('<p><strong>Title: ' +
     result.GetTombstoneDataRestResult.Title + '</strong><br />Year: ' +
     result.GetTombstoneDataRestResult.Dated + '<br />Artist: ' +
     result.GetTombstoneDataRestResult.DisplayName + '</p>');

     }
     });

Looks like you want to try this too:

var replies = data;
$.each(replies.Updates ,function(group,value) {
smoore4
  • 3,467
  • 2
  • 23
  • 45
  • Please, note that `$replies = data.Updates;` is already where code goes down. `data` is undefined. So it's not a problem with each loop or correctly referencing json data, as basically in IE there's no data to refer to (it works fine in FF as I said already). – MarcinWolny May 15 '13 at 18:47
0

You have a character called &#8203; in your JSON.
See a description here: What's HTML character code 8203?

It's right before your colon here "time"​:

Can you clean your output and validate the JSON and try again?

Community
  • 1
  • 1
Tuan
  • 1,396
  • 12
  • 23
  • I think it's just an issue with copy&pasting. JSON code is outputted in PHP using `json_encode($output);` where `$output` is an array. – MarcinWolny May 16 '13 at 08:49