Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/472.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Ajax请求没有';不能使用弹簧控制器_Java_Javascript_Jquery_Ajax_Spring Mvc - Fatal编程技术网

Java Ajax请求没有';不能使用弹簧控制器

Java Ajax请求没有';不能使用弹簧控制器,java,javascript,jquery,ajax,spring-mvc,Java,Javascript,Jquery,Ajax,Spring Mvc,无法理解为什么此代码总是在控制台中打印“Error!” 这是我的弹簧控制器 @RequestMapping("/spinner") public class SpinnerController { @RequestMapping(method = RequestMethod.GET, produces = "application/json") public @ResponseBody String spinner() throws In

无法理解为什么此代码总是在控制台中打印
“Error!”

这是我的弹簧控制器

@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>
控制台输出:

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

默认情况下,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'

console“Error!”-在哪里?在浏览器控制台或服务器控制台中?传递给$.ajax()的“error”函数接收3个参数:(jqXHR jqXHR、String textStatus、String errorshown)。尝试将它们添加到中并检查它们的值。从后端返回的输出应为JSON格式,而不是textDamian格式。请尝试发送“{a:1}”而不是“应答”确定。我修正了这个错误。
@RequestMapping("/spinner")
public class SpinnerController {

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