Drongo_III Posted August 7, 2012 Share Posted August 7, 2012 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! Quote Link to comment https://forums.phpfreaks.com/topic/266766-ajax-response/ Share on other sites More sharing options...
Christian F. Posted August 7, 2012 Share Posted August 7, 2012 [*]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. Quote Link to comment https://forums.phpfreaks.com/topic/266766-ajax-response/#findComment-1367497 Share on other sites More sharing options...
Drongo_III Posted August 7, 2012 Author Share Posted August 7, 2012 Thanks ChristianF - looks like i've invented resonseJson then 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? [*]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. Quote Link to comment https://forums.phpfreaks.com/topic/266766-ajax-response/#findComment-1367608 Share on other sites More sharing options...
Christian F. Posted August 8, 2012 Share Posted August 8, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/266766-ajax-response/#findComment-1367665 Share on other sites More sharing options...
Drongo_III Posted August 8, 2012 Author Share Posted August 8, 2012 Thanks for the help Quote Link to comment https://forums.phpfreaks.com/topic/266766-ajax-response/#findComment-1367735 Share on other sites More sharing options...
Christian F. Posted August 8, 2012 Share Posted August 8, 2012 You're welcome. Quote Link to comment https://forums.phpfreaks.com/topic/266766-ajax-response/#findComment-1367764 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.