1

I've literally been trying to debug this for a full day and it's utterly bewildering. looking for some hints.

i have these functions inside a javascript namespace:

var ZZ = {
  bar: function(data) {
    console.log(data);
  },
  foo: function() {
    $.ajax({
      'url':'/test',
      'type':'GET',
      'dataType':'jsonp',
      'success':function(ret_json, status) {
        console.log(ret_json);   // SUCCESS_1
        console.log(ret_json[1].title);   //SUCCESS_2
        bar(ret_json);
      }
    });
  },
...
}

so i look at the 'net' tab of developer tools and the json returned is correct.. something like:

[{'id':1, 'url':'http://a.com', 'title':'hi'}, {'id':2, 'url':'http://b.com', 'title':'hi2'}]

now i look at the console and see that at SUCCESS_1, it prints out something like:

[{'id':1, 'url':'http://c.com', 'title':'hi3'}, {'id':2, 'url':'http://c.com', 'title':'hi3'}]

as you can see, the id is correct, but url and title is not. (i can also change the ids in the database and they will be correctly carried through)

then the next line at SUCCESS_2, it grabs the correct title! 'hi2'

next i put a debugger statement in to the success function: ret_json looks totally correct, but when i step into the bar() function.. the data looks wrong again.

next i add:

var hi = {};
console.log(hi);

into the success function.. it prints out.. (not joking)

{'url':'http://c.com', 'title':'hi3'}

so basically, this "default" is overwriting whatever was actually there.

i've added cache:false to the ajax call, i've tried json and jsonp, i'v tried clearing cache, i've tried using chrome/safari/firefox.. but nothing doing and i doubt it's something that obvious... it's jsut really really effed up.

any other ideas of what I can try?


UPDATE: adding this namespace to a different html page. the most egregious example of

var hi = {}
console.log(hi)

printing stuff is gone but the original problem of the ret_json having wrong data is still present. (i still don't have access to a working version of the previous html page)

ggez44
  • 895
  • 8
  • 17
  • The thing with console.log(hi) is definitely weird, can't say anything about that right away, but have you tried removing all external scripts besides jquery and your sample script? Also, try running the same short piece of code in console... – Dmitry Pashkevich Jun 25 '12 at 19:27
  • yes, this ajax call works fine if i move it to a page by itself.. it's definitely some weird interaction with something else in my current javascript environment... just not sure where i should look. – ggez44 Jun 25 '12 at 19:59
  • Is this somehow related to single quotes in your JSON? – veritasetratio Jun 25 '12 at 20:52
  • clear your browser cache, and make sure that jQuery is not caching the server data either. – zzzzBov Jun 25 '12 at 21:07
  • @veritasetratio can't imagine it being quotes related.. not sure how i'd force json to use double quotes for strings anyway – ggez44 Jun 25 '12 at 21:10
  • @zzzzBov tried clearing cache already.. even used a new browser that had never touched this js in the past – ggez44 Jun 25 '12 at 21:11

2 Answers2

2

@ggez44, this is because the console.log is not a synchronized call in Chrome/Firefox/Safari. There is usually a delay of a few ms.

It looks on my machine the delay is not constant, and is between 4ms and 16ms in Chrome, and about 2ms in Safari.

http://techblog.appnexus.com/2011/webkit-chrome-safari-console-log-bug/

Bizarre console.log behaviour in Chrome Developer Tools

My speculative solution to this is to do something like "console.log(a.toJSON());". Then you end up with a pointer to the string object passed in, which is not tied to the original object.

Update: Apparently this is a "feature, not a bug". There are a few quite decent links into the tickets in webkit mailing list. Why does javascript object show different values in console in Chrome, Firefox, Safari?

Community
  • 1
  • 1
Alex Dong
  • 975
  • 2
  • 9
  • 18
  • wow yeah i was totally setting it to those "default" values in the 'bar' function. (was generated so grep didn't hit).. but since it was after my console.log calls, i didn't even bother looking. Also it's not totally working in Firefox either.. the console logs the right objects, but if you click down into the details of those objects, the data is again time forwarded – ggez44 Jun 25 '12 at 22:14
  • Great finding! Thanks, Alex, I was curious too! – Dmitry Pashkevich Jun 26 '12 at 08:53
1

Did you, anywhere in your code, add the following?

Object.prototype.url = "http://c.com";
Object.prototype.title = "hi3";

This is the only reason those properties would show up when you create an empty object.

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
  • +1; @ggez44 To verify this, run `console.log(Object.prototype)` and see if those properties are present. – apsillers Jun 25 '12 at 19:32
  • thanks, i did greps for the offending data but didn't find anything, but i will try printing out the object prototype... something else is broken in my branch now, but will come back with the result soon. thanks much! – ggez44 Jun 25 '12 at 20:01
  • nope.. nothing special in the prototype. checked Object.prototype as well as looking at the instance's prototype (the instance that has the weird values) – ggez44 Jun 25 '12 at 20:45