1

我已经环顾了一段时间,看到了许多类似的问题,但没有任何帮助。我有一个 getJSON 调用,它调用我的 Spring 控制器并以 JSON 文本响应(已验证确实返回了 JSON 文本),但从未执行回调(基于回调函数中没有执行任何操作,并且我没有收到错误的错误JavaScript)。

在我的jsp文件中:

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

在我的控制器中:

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

    if (user == null)
        return null;

    return createUserJSON(user);
}
4

3 回答 3

0

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.

于 2012-09-07T20:20:07.320 回答
0

这意味着 JSON 无效。可能是内容无效或内容类型设置不正确......

$.getJSON has no error callback

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

看看你需要使用什么问题

$.ajax({
  url: "myurl",
  type: "GET",
  dataType: "json",
  success: function() {
    //called when successful
  },
  error: function(e) {
    //called when there is an error
  },
});
于 2012-09-07T20:24:23.780 回答
0

找到了答案。结果证明 JSON 必须是有效的。我犯了一个错误,所以 JSON 格式不正确。我什至在回调函数之前都不知道格式是否重要。

于 2012-09-07T20:29:23.967 回答