0

I am trying to use jQuery's $.getJSON() function to make a server-side AJAX call:

$.getJSON() {
    url: "/my-server/some-url",
    success: function() {
        alert("Success!");
    }
}

Firebug shows this as a JavaScript error:

SyntaxError: missing ; before statement
[Break On This Error]   

$.getJSON() {

In reading the tutorial it appears that I'm using it right, passing in a URL and then a success callback. This particular URL doesn't require any data parameters, so I omitted them. Can anyone spot where I'm going awrye? Thanks in advance!

5 Answers 5

5

You want to call a function, using the getJSON arguments:

$.getJSON("/my-server/some-url", function(data) {
        alert("Success!");
});

Or use $.ajax passing an object:

$.ajax({
    url : "/my-server/some-url",
    dataType : 'json',
    success : function(data) {
            alert("Success!");
    }
});

To pass data (e.g. { dog: 1, cat: 4 } which will be converted to URL parameters) you can pass the object directly to both functions:

$.getJSON("/my-server/some-url", { dog : 1, cat : 4 }, function(data) {
        alert("Success!");
});
// Or
$.ajax({
    url : "/my-server/some-url",
    dataType : 'json',
    data : { dog : 1, cat : 4 },
    success : function(data) {
            alert("Success!");
    }
});
Sign up to request clarification or add additional context in comments.

4 Comments

I was using a wrong signature first using $.getJSON instead of $.ajax.
@user1689607 Because the answer was wrong before it was corrected through edits.
Thanks @Daff - please see my question under Aktee's answer - I have the same question for you!
I elaborated a little more with an example how to pass data.
2

Your code is wrong. Try this:

$.getJSON("/my-server/some-url",function() {
         alert("Success!");
     }
});

Your code is closer to the $.ajax method.

$.ajax({
    url: "/my-server/some-url",
    success: function() {
        alert("Success!");
    }
});

EDIT:

You can also pass URL arguments with the data parameter.

http://api.jquery.com/jQuery.getJSON/

Data that is sent to the server is appended to the URL as a query string. If the value of the data parameter is an object (map), it is converted to a string and url-encoded before it is appended to the URL.

$.getJSON("/my-server/some-url",{ dog: 1, cat: 4 },function() {
         alert("Success!");
     }
});

// Requests: "/my-server/some-url?dog=1&cat=4

1 Comment

Thanks @jimp - please see my question under Aktee's answer - I have the same question for you!
1

You are not using the correct syntax. $.getJSON is a function with parameters. The first parameter is mandatory, and it's a string containing the url.

$.getJSON(
    "/my-server/some-url", 
    data,
    function(callback) { 
        alert('success')
    }
);

$.getJSON is a shortcut for $.ajax, which is similar to what you wrote. $.ajax takes a single object as a parameter..

$.ajax({
  url: url,
  dataType: 'json',
  data: data,
  success: callback
});

Source: http://api.jquery.com/jQuery.getJSON/

2 Comments

Thanks @Aktee (+1) - question - how do I set data parameters with $.getJSON? Let's say I want to use it with a new URL that expects both a dog and a cat data parameter. Where you right data could I just use { dog: 1, cat: 4 } or is there some other trick? Thanks again!
@pnongrata, It is exactly how you did it! $.getJSON("/url/here", {dog: 1, cat: 1}, callback)
0

http://api.jquery.com/jQuery.getJSON/

$.getJson('url', function(data) {
   //do stuff with data
});

Comments

0

The .getJSON function is a shorthand for the .ajax function

jQuery.getJSON( url [, data] [, success(data, textStatus, jqXHR)] )

$.getJSON("/my-server/some-url", function() {
  //Success
  alert("success");
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.