6 Replies - 500 Views - Last Post: 28 January 2012 - 01:01 PM Rate Topic: -----

Topic Sponsor:

#1 CodedTragedy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 23-January 12

Getting JSON with JQuery?

Posted 27 January 2012 - 10:34 PM

I initially thought this was going to be easy but apparently not. I can't seem to get the JSON that is returned from a webpage. I've tried the following code with no avail:
$.getJSON(siteAddress, function(data) {
   $.each(data.items, function(i, item) {
        $(".test").text(item);
    });
});



I've tried this too:
$.ajax({
   type: "GET",
   url: site,
   dataType: "html",
   success: function(msg) {
      alert(msg);
   }
});



Also I tried doing an XMLHttpRequest() an nothing happens. Any Ideas why?

Is This A Good Question/Topic? 0
  • +

Replies To: Getting JSON with JQuery?

#2 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1758
  • View blog
  • Posts: 2,693
  • Joined: 08-June 10

Re: Getting JSON with JQuery?

Posted 27 January 2012 - 10:44 PM

The first thing that comes to mind is that you aren't handling the error, if an error should be occurring. Try this and see what happens:
$.ajax({
    type: "GET",
    url: site,
    dataType: "json",
    success: function(msg) {
        alert(msg);
    },
    error(jqXHR, textStatus) {
        alert("AJAX failed: " + textStatus);
    }
});



It's also a good idea to examine the actual AJAX request in your browser, using something like Firefox's Firebug addon, or the dev tools included in Chrome, Opera and IE. That will let you examine exactly what is happening.
Was This Post Helpful? 0
  • +
  • -

#3 CodedTragedy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 23-January 12

Re: Getting JSON with JQuery?

Posted 28 January 2012 - 09:30 AM

Well I tried what your said and all it returns as a "error". Basically what i'm trying to do is get the returned JSON data from this site:

http://www.education...seewhatyoucando

To see the JSON data via the browser I type in:
http://api.education.com/service/service.php?resf=json&f=districtSearch&sn=sf&key=<enter api key here>&zip=90210



Any help will be appreciated.
Was This Post Helpful? 0
  • +
  • -

#4 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 3239
  • View blog
  • Posts: 10,644
  • Joined: 18-April 07

Re: Getting JSON with JQuery?

Posted 28 January 2012 - 12:32 PM

Uhhh... you do realize your ajax call has a data type of "html" and not "json" like it should be right? By specifying the datatype as HTML the response is not going to be treated as a json reply.

:)
Was This Post Helpful? 0
  • +
  • -

#5 CodedTragedy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 23-January 12

Re: Getting JSON with JQuery?

Posted 28 January 2012 - 12:39 PM

Yes actually, I edited my original post. Didn't realize I had typed "html". In my code I tried using "json" and "application/json" as the dataType and it still doesn't work.

lol nvm I can't edit my post sorry guys!
Was This Post Helpful? 0
  • +
  • -

#6 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 3239
  • View blog
  • Posts: 10,644
  • Joined: 18-April 07

Re: Getting JSON with JQuery?

Posted 28 January 2012 - 12:59 PM

Silly question but you got yourself an API key right? if so, have you made a simple request through the browser and gotten results?

:)
Was This Post Helpful? 0
  • +
  • -

#7 CodedTragedy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 23-January 12

Re: Getting JSON with JQuery?

Posted 28 January 2012 - 01:01 PM

Hehe it's not silly, i've known devs that have made mistakes like that. But yes I do have an api key, and i've tested it out in the browser and receive the proper JSON response :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1