简体   繁体   中英

$.getJSON With Spring Not Executing Callback

I've looked around for a while now, seen many similar problems, but none that help. I have a getJSON call that calls my Spring controller and responds with JSON text (Verified that JSON text is indeed being returned), but the callback is never executed (Based that nothing executes within the callback function and I don't receive errors with bad JavaScript).

In my jsp file:

function getUserText(str)
{
    $.getJSON("selectUser.htm", { id: str }, function(user)
    {
        //Doesn't matter what's here
    });
}

In my controller:

@RequestMapping(value="/selectUser.htm")
public @ResponseBody String SelectUser(@RequestParam String id)
{
    Users user = userMap.get(id);

    if (user == null)
        return null;

    return createUserJSON(user);
}

I'm not sure about this, but my guess is the function you provide is the success function that gets called when ajax returns. It is possible that the request is not returning successfully.

It means the JSON is invalid. It could be the content is invalid or the content-type is not correctly set....

$.getJSON has no error callback

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

to see what the problem is you need to use

$.ajax({
  url: "myurl",
  type: "GET",
  dataType: "json",
  success: function() {
    //called when successful
  },
  error: function(e) {
    //called when there is an error
  },
});

Found the answer. Turns out that the JSON needs to be valid. I made a mistake so the JSON wasn't formatted correctly. I didn't know that the format mattered even before the callback function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM