0

Hey I'm trying to retrieve data from a json file I have hosted on my server but it's not working, I'm not sure what is wrong as I'm not the most well versed in this topic. Any tips are appreciated!

$(document).ready(function () {
$("#button").click(function () {
    $.getJSON({
        type: "POST",
        url: "some URL will go here",
        success: function (result) {
            $("#div1").append(result);
        }
    });
});
});

heres the fiddle as well: http://jsfiddle.net/ahuston12/E5SzH/

Allan Huston
  • 17
  • 1
  • 7
  • Have you checked that the JSON file is accessible from the outside (e.g., can you open it in a browser)? – Ulrich Schmidt-Goertz Jul 18 '13 at 11:46
  • Did u checked what type of response/ error you are getting. – Vignesh Jul 18 '13 at 11:47
  • 1
    open the js console (eg. ctrl+shift+j in chrome) and check whether the json resource is loaded. – collapsar Jul 18 '13 at 11:47
  • for getting JSOn values use "result.d" – Vignesh Jul 18 '13 at 11:48
  • the JSON is definitely accessible, I also tried other json files that I know are working and I get the same problem – Allan Huston Jul 18 '13 at 11:49
  • What message appears in the networking tab of the chrome dev tools? – starbeamrainbowlabs Jul 18 '13 at 11:54
  • gives me back [object Object], Method: Option, Status: Pending – Allan Huston Jul 18 '13 at 12:10
  • i think you have mixed up function signatures. see [here](http://api.jquery.com/jQuery.getJSON/) for details - your call should actually be `$.ajax(...`instead of `$.getJSON(...` or you need to change the arguments to `$.getJSON(, )`. btw, do you really use the `POST` method (if you query the webservice with a tailored url, you don't) ?. – collapsar Jul 18 '13 at 12:17
  • @AllanHuston: does [this fiddle](http://jsfiddle.net/collapsar/Hr5LE/) work woth your url to the json service ? – collapsar Jul 18 '13 at 12:26
  • You could be right, like I said I'm just trying to get this work with little experience. Is there anything else in the function that's incorrect if I use the $.ajax signature – Allan Huston Jul 18 '13 at 12:31
  • @AllanHuston: you most certainly need to preprocess `result`into somenthing that you can actually insert into the dom. `result`will contain an object (ie. a nested object structure which the json notation represents). have a look at [this fiddle(http://jsfiddle.net/collapsar/Hr5LE/)](http://jsfiddle.net/collapsar/Hr5LE/), it's a working example using a public json web service as data source. – collapsar Jul 18 '13 at 12:33
  • I'll definitely have to try this method thanks! – Allan Huston Jul 18 '13 at 12:42

4 Answers4

0

Open a modern browser like Firefox or chrome and open the developer tools. Navigate to the page that contains the above code and monitor the "network" tab to see the related http request triggered via ajax. You can then see the request + response including header, body and return codes. This should help to figure out the problem.

eX0du5
  • 896
  • 7
  • 16
  • It looks like it's not loading properly but I'm not sure why. It doesn't even go to my link it goes to my desktop apparently? gives me back [object Object], Method: Option, Status: Pending – Allan Huston Jul 18 '13 at 11:51
  • Maybe you can post the link you are trying? What is the HTTP response code? – eX0du5 Jul 18 '13 at 11:53
0

You should show us what's going on the server side. It doesn't seems to be problems on this jquery code so i guess it's from server.

You can check the response value to see the problem : on chrome ctrl+j -> network. You'll see your request, click on it to see details like the server's response.

Yabada
  • 1,728
  • 1
  • 15
  • 36
0

If you have FireBug installed in your Mozilla Firefox open it, in that you can see Net Tab . If you click that you can able to see what kind of response you are getting. I ahve attached the Image For your reference. And for getting a JSON result use "result.d".

Ajax Response Image

Vignesh
  • 1,458
  • 12
  • 31
  • 59
0

The url that you are using doesn't return a json response, it returns a javascript file that charge a json result into a variable, because of that you can't get the result.. if you try with other site that returns information in json format like this you are going to get the data:

$.getJSON("http://headers.jsontest.com/", function (result) {
            console.log(result);
        }
    );
Hernandcb
  • 530
  • 8
  • 20