Jump to content

Ajax response


Drongo_III

Recommended Posts

HI Guys

 

 

Couple of questions:

 

1) When working with ajax in 'raw' javaScript (i.e. not jquery), if the response from the server is going to be in Json format do you need to make the onreadystate response:

 

http.responseJson

 

Or since the json is essentially a string do you just do get request 'responseText' and then decode the string using json parser.

 

Which brings me to question two!

 

2) Is it ok to rely on native browser support to decode jason - i.e. using:

 

var myObject = JSON.parse(myJSONtext);

 

Or is this a bad idea if someone is using an older browser? And is it therefore better to rely on a library instead?

 

Many thanks

 

Drongo!

 

Link to comment
Share on other sites

  1. [*]As far as I'm aware there is no
http.responceJSON attribute, only http.responseText, when working outside of any frameworks.

So decoding the string with a JSON parser is indeed the correct approach.

[*]A browser either supports JSON parsing, or it doesn't. If it doesn't then you get a catchable exception, in which case you can use a failsafe method. Libraries doesn't really enter into the equation ant this point, except for the fact that they tend to handle the error caused by a missing JSON parser.

 

Relying on a library, or not, is a decision which requires considerations of a lot more points that just JSON parsing. That said, it is generally recommended to use one (like jQuery) if you need to do a lot of DOM manipulation and AJAX calling. Simply because it lessens the workload on you, mostly due to the fact that most/all of the browser incompatibilities are handled automatically.

If you need only a few simple AJAX calls, or some other trivial functionality, then it can be advisable to forgo the library. It's all a judgement call, really.

Link to comment
Share on other sites

Thanks ChristianF - looks like i've invented resonseJson then  :D

 

The reason I thought a library might be preferable was because then you wouldn't rely on the browser to decode - instead the library would ensure that the decoding would work. But I could be wrong on this. Does that make sense as an approach to ensure legacy browsers still work?

 

 

 

 

  1. [*]As far as I'm aware there is no
http.responceJSON attribute, only http.responseText, when working outside of any frameworks.

So decoding the string with a JSON parser is indeed the correct approach.

[*]A browser either supports JSON parsing, or it doesn't. If it doesn't then you get a catchable exception, in which case you can use a failsafe method. Libraries doesn't really enter into the equation ant this point, except for the fact that they tend to handle the error caused by a missing JSON parser.

 

Relying on a library, or not, is a decision which requires considerations of a lot more points that just JSON parsing. That said, it is generally recommended to use one (like jQuery) if you need to do a lot of DOM manipulation and AJAX calling. Simply because it lessens the workload on you, mostly due to the fact that most/all of the browser incompatibilities are handled automatically.

If you need only a few simple AJAX calls, or some other trivial functionality, then it can be advisable to forgo the library. It's all a judgement call, really.

Link to comment
Share on other sites

If you're concerned about legacy browsers being used to access the site, then yes; It would be a reasonable approach.

Though, that said I would recommend you to read up a bit more on JSON, its syntax and browser support. The more you know about a subject, the easier it'll be to make the right decision.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.