Search code examples
javaajaxjsonp

jsonp call: missing ) after argument list


I am calling a rest API through Jsonp. The API server returns the right value, but I am getting 'missing ) after argument list' and ajax returns error. What is not correct here?

In Javascript,

$.ajax({
url: 'http://localhost:8080/version',
dataType: 'jsonp',
type: 'GET',
success: function (data) {
    console.log(data);
},
error: function(xhr, status, error){
     console.log(xhr.status + ": " + xhr.responseText)
}, 

});

In Java,

@GET
@Produces("application/javascript")
public String getVersion(@QueryParam("callback") String callback) {
      return callback + "(hello)";
}

Solution

  • I can't see how you'd get that error, but the JSONP response is wrong. You're sending back something like this:

    callbackName(hello)
    

    That expects a global hello variable; hello should be in quotes:

    callbackName("hello")
    

    So:

    @GET
    @Produces("application/javascript")
    public String getVersion(@QueryParam("callback") String callback) {
          return callback + "(\"hello\")";
    // NOTE ------------------^^-----^^
    }
    

    Regarding

    missing ) after argument list

    This is the kind of thing that would produce that error:

    callbackName("hello"
    

    or a missing , between arguments would also produce it:

    callbackName("hi" "there")