简体   繁体   English

Ajax请求不适用于Spring控制器

[英]Ajax request doesn't work with Spring controller

Can't understand why this code always prints in console "Error!" 无法理解为什么此代码总是在控制台"Error!"打印 .

Here is my Spring controller 这是我的弹簧控制器

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

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

And my JS script: 还有我的JS脚本:

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

And JSP page: 和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>

Any ideas why I get an error? 任何想法为什么我会出错?

UPD UPD

Here is log for script's method error 这是脚本方法错误的日志

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

Console output: 控制台输出:

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

UPD2 UPD2

Fixed with adding this code: 通过添加以下代码进行修复:

@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;
    }
}

JQuery by default is trying to guess what kind of data are coming in XHR response using MIME type. 默认情况下,JQuery试图猜测使用MIME类型的XHR响应中将传入哪种数据。 Data returned by Spring does not match MIME type you are sending. Spring返回的数据与您发送的MIME类型不匹配。 You can change your backend method to: 您可以将后端方法更改为:

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

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

You can also force jQuery not to look into MIME type (not a great idea) and set dataType : 'text' in your ajax object. 您还可以强制jQuery不要查看MIME类型(这不是一个好主意),并在ajax对象中设置dataType : 'text'

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

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