0

I am trying .ajax to hit a URL which returns a list of object, I am using jacksonJsonView in spring to it returns json in browser. But when I try this code it never goes into the success, but the error alert shows the textstatus to be 'parseerror'. the alert looks like this: 'status=parsererror,error=jQuery15109695890768120119_1357924928198 was not called'

$(function() {  
$("#tags").autocomplete({
source: function( request, response ) {
    $.ajax({
        url: 'http://localhost:8181/jquery/api/states/regex?stateName='+request.term,
        method: 'GET',
        dataType: 'jsonp',
        success: function(json) {
        alert("test");

        },
        error: function(httpRequest, textStatus, errorThrown) { 
        alert("status=" + textStatus + ",error=" + errorThrown);
        }
    });
}
})

The API returns some thing like this:

[

    {
        "id": 12,
        "stateName": "Vermont",
        "intPtLon": -72.673354,
        "intPtLat": 44.0605475,
        "stUsps": "VT"
    },
    {
        "id": 20,
        "stateName": "Virginia",
        "intPtLon": -78.6681938,
        "intPtLat": 37.5222512,
        "stUsps": "VA"
    }

]

Adding the following line fixed the issue, it works fine in IE bit not so much in firefox

3
  • 1
    Are you sure you need a JSONP? Commented Jan 11, 2013 at 17:31
  • If I remove it, it gives "status=error,error=" Commented Jan 11, 2013 at 17:35
  • See my answer. Suppose changing dataType: 'jsonp', to dataType: 'json', will be completely enough. Commented Jan 11, 2013 at 17:38

1 Answer 1

3

You should change

dataType: 'jsonp',

to simple

dataType: 'json',

Or, if you really need JSONP and can't use JSON - you should take callback parameter from request on server side and wrap response like this:

Request["callback"] + "(" + string_with_json_response + ")";

So, in results you will get something like:

jQuery15109695890768120119_1357924928198('{"response":"val"}');
Sign up to request clarification or add additional context in comments.

9 Comments

Changing jsonp to json gives me "Status=error,error=". and where do I add the Request["callback"] + "(" + string_with_json_response + ")"; ?
Hm. That is strange. Response you shown in your question looks like valid JSON. Take a look at network tab of developer tools in browser. You should find your ajax request there after it is executed and take response from there. Response you provided looks to be Ok and it should work fine with dataType":'json'. About where to add - I can give only direction. It should be at the same place where current response is returned. Not sure how you do that in Java.
everything looks fine in firebug/net, The response header looks like this Content-Type application/json Server Jetty(6.1.22) Transfer-Encoding chunked
Does the [] around the json matter? not sure how to remove it. in some posts they suggest that it could be the issue
Hm. That should not be a problem at all. That is a valid JSON array. Have you checked plain response text? Possibly you have some garbage there which you do not see, but it breaks jQuery
|

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.