简体   繁体   English

$ .getJSON与Spring不执行回调

[英]$.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). 我有一个getJSON调用,该调用调用我的Spring控制器并以JSON文本作为响应(已验证确实返回了JSON文本),但从未执行过回调(基于在回调函数中未执行任何操作,并且我不会收到错误提示) JavaScript)。

In my jsp file: 在我的jsp文件中:

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. 我不确定,但是我猜您提供的功能是ajax返回时调用的成功功能。 It is possible that the request is not returning successfully. 该请求可能无法成功返回。

It means the JSON is invalid. 这表示JSON无效。 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. 原来,JSON需要有效。 I made a mistake so the JSON wasn't formatted correctly. 我弄错了,所以JSON的格式不正确。 I didn't know that the format mattered even before the callback function. 我什至不知道格式是否在回调函数之前就很重要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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