Skip to main content
Topic: jQuery, JSON and Chrome/Firefox (Read 8318 times) previous topic - next topic
0 Members and 1 Guest are viewing this topic.

jQuery, JSON and Chrome/Firefox

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:
Code: [Select]
			if (typeof request.answers != 'undefined')
{
$.each(request.answers, function(i, item) {
alert(item.single_status);
});​
}

...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?
Bugs creator.
Features destroyer.
Template killer.

Re: jQuery, JSON and Chrome/Firefox

Reply #1

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.

Re: jQuery, JSON and Chrome/Firefox

Reply #2

Looking at the error log (thanks @The Craw for suggesting <= we still have some names duplication... :-\) I noticed that the file has a strange:
Code: [Select]
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:-))
Bugs creator.
Features destroyer.
Template killer.

Re: jQuery, JSON and Chrome/Firefox

Reply #3

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. >_<
Bugs creator.
Features destroyer.
Template killer.

Re: jQuery, JSON and Chrome/Firefox

Reply #4

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:

Code: [Select]
var MyClass = function()
{
    this.each = function(elements, callbackFunction)
    {
        for (var i = 0; i < elements.length; ++i) {
            callbackFunction(i, elements[i]);
        }
    }
}

var $ = new MyClass;

var myArray = [1, 2, 4];
$.each(myArray, function(index, value) {
    console.log(value);
});
~((0040 + 0x20) + (0b111 << 1))
"You never said something, for me, to penny mention work a barbecue pit." ~ Gilligan

Re: jQuery, JSON and Chrome/Firefox

Reply #5

Isn't it that $() needs to have a jQuery object and $.each() can use any object?

Re: jQuery, JSON and Chrome/Firefox

Reply #6

Hi Everyone, been stalking the site from sometime and it looks really nice.

Quote from: emanuele – 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:
Code: [Select]
 if (typeof request.answers != 'undefined')
 {
 $.each(request.answers, function(i, item) {
 alert(item.single_status);
 });
 }

...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

Code: [Select]
if (typeof(request.answers) !== 'undefined') {
$.each(request.answers, function(i, item) {
alert(item.single_status);
});​
}


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.
Hi Spuds,
$.each is used to iterate over objects/arrays/array like objects.

Internally it checks for array/object first and then make a loop over it. I personally use 'for in' loop for objects and 'for' loop for ararys.


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:
Code: [Select]
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

Code: [Select]
(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.

Re: jQuery, JSON and Chrome/Firefox

Reply #7

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!

Re: jQuery, JSON and Chrome/Firefox

Reply #8

 emanuele slaps @Joker™ for fun! <= I didn't realise I can now slap people on the forum too and not only on IRC!! LOL

 emanuele 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
Bugs creator.
Features destroyer.
Template killer.

Re: jQuery, JSON and Chrome/Firefox

Reply #9

Quote from: Spuds – 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!
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 –
 emanuele 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.

Re: jQuery, JSON and Chrome/Firefox

Reply #10

Quote from: Joker™ – Lol ,seems like coding fever has made @emanuele completely mad :-X.
 emanuele was already mad to begin with. O:-)

Quote from: Joker™ – Also, I've just noticed few issue, with the editor and mentions functionality, and will be reporting them on gitHub.
Cool, thanks! :D
Bugs creator.
Features destroyer.
Template killer.