• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

applying ajax on json data  RSS feed

 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
I've been trying to get this example working from http://www.tutorialspoint.com/json/json_ajax_example.htm . However, I do not see any change when I click the button.

Please find the code below.
Using my debug statements, I think it's failing at the line var jsonObj = JSON.parse(http_request.responseText);
How do I get it to work?



Thanks.
 
Sheriff
Posts: 67257
170
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you used the browser's debugger to see what the response text is? Is it valid JSON?
 
Bear Bibeault
Sheriff
Posts: 67257
170
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
P.S. You should be using jQuery to do your Ajax. Much simpler and more robust.
 
Aftab Hassan
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Bear Bibeault wrote:P.S. You should be using jQuery to do your Ajax. Much simpler and more robust.


Thank you, I've just started learning ajax using jQuery.

Bear Bibeault wrote:Have you used the browser's debugger to see what the response text is? Is it valid JSON?


Please can I get some more explanation here as I'm not aware of how to use the browser's debugger to see the response.
I tried opening the link in the code above which is http://www.tutorialspoint.com/json/data.json and I got some valid JSON. Please explain.
 
Rancher
Posts: 42974
76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm guessing that you created the HTML/JavaScript (JS henceforth) file locally, but that the page is still trying to access the JSON at http://www.tutorialspoint.com/json/data.json; is that correct? If so, you're running into the Same-origin policy restriction.

Note how it works fine on that example page, because both HTML/JS and JSON URLs are on the same domain. If you want to run the HTML/JS locally, you also need to have the JSON locally (and change the JS code accordingly).

As to using the browser's debugger, that may be a bit advanced at the moment, but start by checking JS errors: in Firefox, go to Tools -> Web Developer -> Browser Console. There you can see any JavaScript errors (like you would have gotten in this case).
 
Bear Bibeault
Sheriff
Posts: 67257
170
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'll respectfully disagree with Ulf on the "advanced" part -- I think that as soon as someone writes their first line of JavaScript, that they should have the browser's debugger open so that they can at minimum see any errors in the JavaScript console.

By the time they've advanced to using Ajax, they should be get familiar with the Network tools in the browser. Otherwise, they are flying blind.

And in my opinion, use the Chrome debugging tools as the first line of defense. I find them somewhat superior to Firebug.
 
If you try to please everybody, your progress is limited by the noisiest fool. And this tiny ad:
ScroogeXHTML - small and flexible RTF to HTML converter library
https://coderanch.com/t/710903/ScroogeXHTML-RTF-HTML-XHTML-converter
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!