简体   繁体   中英

Ajax request doesn't work with Spring controller

Can't understand why this code always prints in console "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:

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:

<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

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

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. Data returned by Spring does not match MIME type you are sending. 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.

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