General => Chit Chat => Topic started by: emanuele on January 15, 2014, 05:02:12 pm
Title: jQuery, JSON and Chrome/Firefox
Post by: emanuele on January 15, 2014, 05:02:12 pm
This evening I was playing with jQuery "AJAX" and JSON.
Simple call to the server, returns a JSON response with a certain amount of data.
One of those is an array that may, or may not contain something. Since if it contains something I have to loop over it, but if it doesn't I have just to skip it, I was doing some debugging using the following code:
...heck, using it in Chrome of Firefox killed completely the entire file, but not on the response, while loading the page! Firefox was not even showing the file as load, while Chrome was showing the file, but it didn't execute a single line of it. Commenting out the $.each part, everything works again.
Now, am I completely dumb doing such a thing? Or the problem is somewhere else?
Title: Re: jQuery, JSON and Chrome/Firefox
Post by: Spuds on January 15, 2014, 05:27:53 pm
quick thought, what does $ contain in the $.each? I'm used to seeing it as $('selector').each or inside of a function $this.each or the like.
Title: Re: jQuery, JSON and Chrome/Firefox
Post by: emanuele on January 15, 2014, 05:38:24 pm
Looking at the error log (thanks @The Craw for suggesting <= we still have some names duplication... :-\) I noticed that the file has a strange:
Resource interpreted as Script but transferred with MIME type text/x-js:
Apparently for some reason this is the only "reported", but all the javascript files have this type of MIME, dunno if relevant...
I think that when you use it like $.something it is called like a function, similar to $.ajax... I think... (I found the example on google O:-))
Title: Re: jQuery, JSON and Chrome/Firefox
Post by: emanuele on January 15, 2014, 05:47:07 pm
Never mind, got it: it was an empty char of some sort hidden at the end of the last line of the command, just after the semicolon...
In the end, as usual it's just me being dumb. >_<
Title: Re: jQuery, JSON and Chrome/Firefox
Post by: The Craw on January 15, 2014, 06:01:36 pm
Quote from: emanuele – I think that when you use it like $.something it is called like a function, similar to $.ajax... I think... (I found the example on google O:-))
Yeah, that's just calling the 'each' method of the jQuery object contained in the $ variable. As an example:
Simple call to the server, returns a JSON response with a certain amount of data.
One of those is an array that may, or may not contain something. Since if it contains something I have to loop over it, but if it doesn't I have just to skip it, I was doing some debugging using the following code:
...heck, using it in Chrome of Firefox killed completely the entire file, but not on the response, while loading the page! Firefox was not even showing the file as load, while Chrome was showing the file, but it didn't execute a single line of it. Commenting out the $.each part, everything works again.
Now, am I completely dumb doing such a thing? Or the problem is somewhere else?
Hi emanuele,
You should be checking the undefined with the following code, try to match the type also
Quote from: Spuds – quick thought, what does $ contain in the $.each? I'm used to seeing it as $('selector').each or inside of a function $this.each or the like.
Quote from: emanuele – Looking at the error log (thanks @The Craw for suggesting <= we still have some names duplication... :-\) I noticed that the file has a strange:
Resource interpreted as Script but transferred with MIME type text/x-js:
Apparently for some reason this is the only "reported", but all the javascript files have this type of MIME, dunno if relevant...
I think that when you use it like $.something it is called like a function, similar to $.ajax... I think... (I found the example on google O:-))
emanuele, that depends how the function is written, i.e is the function is going through modular concept or the function is utilising the prototyping chain.
The (jQuery or $) is just a constructor function, all instances created with it makes use of inheritance (prototyping for JS). E.g
(function() { var myConstructor = function(params) { // check if instance of constructor already exists or not if (!(this instanceof myConstructor)) return new myConstructor(params);
this.myArg = params; };
// Lets hook up fn with prototype chaining myConstructor.fn = myConstructor.prototype = { init: function() { console.log('initialized'); } };
window.myConstructor = myConstructor; })();
// Lets add a function myConstructor.fn.custPlugin = function() { alert(this.myArg); return this; };
// passing the parameter, and call the function myConstructor('test').custPlugin();
An alert with text 'test' should appear.
Title: Re: jQuery, JSON and Chrome/Firefox
Post by: Spuds on January 17, 2014, 10:15:41 am
Hey Joker, nice to see you around :)
Thanks for the info above, good details.
FYI I changed you name with the tm, it had some odd letters in it, so not sure if we have a utf8 bug in there or what. What did you use to add the tm characters so I can do some digging. Could have been an old issue or
Thanks!
Title: Re: jQuery, JSON and Chrome/Firefox
Post by: emanuele on January 17, 2014, 10:21:30 am
/me slaps @Joker™ for fun! <= I didn't realise I can now slap people on the forum too and not only on IRC!! LOL
/me wonders if it would be nice to have an excerpt of the message in the mentions page...or maybe some ajax loading the message+some context or things like that
Yes, it would be rather cool!! :P
Title: Re: jQuery, JSON and Chrome/Firefox
Post by: Joker™ on January 18, 2014, 02:21:36 am
FYI I changed you name with the tm, it had some odd letters in it, so not sure if we have a utf8 bug in there or what. What did you use to add the tm characters so I can do some digging. Could have been an old issue or
Thanks!
Thanks a lot @Spuds . Even I've noticed the strange chars with my name yesterday only. I've simply copy pasted my name from SMF to elkarte, and it was working fine initially. Not sure what happened with 'TM' recently ::)
Quote from: emanuele – /me slaps @Joker™ for fun! <= I didn't realise I can now slap people on the forum too and not only on IRC!! LOL
Lol ,seems like coding fever has made @emanuele completely mad :-X.
Also, I've just noticed few issue, with the editor and mentions functionality, and will be reporting them on gitHub.
Title: Re: jQuery, JSON and Chrome/Firefox
Post by: emanuele on January 18, 2014, 04:39:36 am