【问题标题】:Ajax request doesn't work with Spring controllerAjax 请求不适用于 Spring 控制器
【发布时间】:2014-08-27 21:58:27
【问题描述】:

不明白为什么这段代码总是打印在控制台"Error!"。

这是我的 Spring 控制器

@RequestMapping("/spinner")
public class SpinnerController {

    @RequestMapping(method = RequestMethod.GET,
                    produces = "application/json")
    public @ResponseBody String spinner() throws InterruptedException {
        Thread.sleep(10);
        return "answer";
    }
}

还有我的 JS 脚本:

function sendRequest() {
    $.ajax({
            url: '/spinner',
            type: 'get',
            contentType: "application/json",
            success: function (resp) {
                alert("Yeah!");
                console.log(resp);
            },
            error: function (){
                console.log("Error!");
            }
        }
    );
}

和JSP页面:

<html>
<head>
    <link type="text/css" rel="stylesheet" href="../resources/css/style.css"/>
    <link rel="stylesheet" href="http://yui.yahooapis.com/pure/0.5.0/pure-min.css">
    <script type="text/javascript" charset="utf-8" src="../resources/js/jquery.js"></script>
    <script type="text/javascript" charset="utf-8" src="../resources/js/send.js"></script>
</head>
<body>
    <button class="pure-button pure-button-primary" onclick="sendRequest()">Press me!</button>
    <div id="spinner">Greeting!</div>
</body>
</html>

知道为什么我会出错吗?

UPD

这是脚本方法错误的日志

error: function (jqXHR, textStatus, errorThrown) {
    console.log(jqXHR);
    console.log(textStatus);
    console.log(errorThrown);
}

控制台输出:

[object Object] send.js:14
parsererror send.js:15
SyntaxError: Unexpected token a 

UPD2

通过添加此代码修复:

@RequestMapping(method = RequestMethod.GET,
                produces = "application/json")
public @ResponseBody Answer spinner() throws InterruptedException {
    Thread.sleep(10);
    return new Answer("info");
}

public class Answer {    
    private String data;

    public Answer(String data) {
        this.data = data;
    }

    public Answer() {
    }

    public String getData() {
        return data;
    }

    public void setData(String data) {
        this.data = data;
    }
}

【问题讨论】:

  • 控制台“错误!” - 在哪里 ?在浏览器控制台还是服务器控制台?
  • 传递给 $.ajax() 的 'error' 函数接收 3 个参数:( jqXHR jqXHR, String textStatus, String errorThrown )。尝试添加它们并检查它们的值
  • 从后端返回的输出应该是 JSON 格式而不是文本
  • Damian 是正确的 - 尝试发送“{a:1}”而不是“answer”
  • 好的。我修复了这个错误。

标签: java javascript jquery ajax spring-mvc


【解决方案1】:

默认情况下,JQuery 会尝试使用 MIME 类型猜测 XHR 响应中的数据类型。 Spring 返回的数据与您发送的 MIME 类型不匹配。您可以将后端方法更改为:

@RequestMapping("/spinner")
public class SpinnerController {

@RequestMapping(method = RequestMethod.GET,
                produces = "application/json")
public @ResponseBody String spinner() throws InterruptedException {
    Thread.sleep(10);
    return "[{\"answer\":1}]";
}
}

您还可以强制 jQuery 不查看 MIME 类型(不是一个好主意)并在您的 ajax 对象中设置 dataType : 'text'。

【讨论】:

    猜你喜欢
    • 2015-03-09
    • 2018-11-24
    • 2016-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-26
    • 2012-12-21
    相关资源
    最近更新 更多