0

I have a page being served from a Web.API app located at http://server/application. On the client side, I am doing a GET to pull some data from the servers. The problem is what I thought should work does not.

This code works:

$.ajax( {
    url: "api/slideid/getdirectories/",
    dataType: 'json',
    success: function ( data ) {
        setPaths( data );
    }
} );

But this does not:

$.getJSON( "api/slideid/getdirectories/",
    function ( data ) {
        setPaths( data );
    } );

In the first example I see in fiddler that the url it is trying to retrieve the data from is http://server/application/api/slideid/getdirectories, which is correct.

In the second, it is http://server/api/slideid/getdirectories, which is not right. I was thinking that these two methods of json GET were the same.... but it seems they are not?

Interestingly, BOTH these methods work on my local dev box- it is only on my staging server that one works and the other does not. IIS settings are identical as far as I can tell- and I dug in pretty good to check.

So I'm wondering why getJSON does not work, when the jQuery docs state that getJSON is just shorthand for the .ajax call?

EDIT: I had put in an explicit version of getJSON hoping to show that they were very similar calls, but the 'real' getJSON call is now there.

Nicros
  • 5,031
  • 12
  • 57
  • 101
  • Go to api/slideid/getdirectories/ from the address bar, see what you get. – Diodeus - James MacFarlane Mar 20 '13 at 18:48
  • 1
    First port of call should always be the documentation, not Stack Overflow. See http://api.jquery.com/jQuery.getJSON/ for correct construction of `getJSON()` call. – Beetroot-Beetroot Mar 20 '13 at 18:50
  • I know that api/slideid/getdirectories doesn't work (as I stated) and I linked the docs to getJSON. I was assuming (maybe incorrectly) that getJSON("api/slideid/getdirectories", function (data) {}) was equivalent to what I wrote- probably wrong there – Nicros Mar 20 '13 at 19:06

2 Answers2

6

You have a wrong implementation of $.getJSON() This should be:

$.getJSON(url, {data:data}, function(data){
     alert(data);
});

where {data:data} is optional.

From the docs:

This is a shorthand Ajax function, which is equivalent to:

$.ajax({
  dataType: "json",
  url: url,
  data: data,
  success: success
});
Community
  • 1
  • 1
Jai
  • 74,255
  • 12
  • 74
  • 103
0
  $.getJSON(url, {data:data}, ....

wrong syntax

PSR
  • 39,804
  • 41
  • 111
  • 151