weixin_33743880 2016-02-24 20:50 采纳率: 0%
浏览 7

Spring MVC Ajax开机自检

I'm trying to post a JSON Object, with Ajax, to an Rest Spring MVC Controller, but I met some troubles.

Let's present you my code:

Controller

@RequestMapping(value = "/register/checkUsername.html", method = RequestMethod.POST,  produces=MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody JsonResponse checkUsername(@RequestBody JsonUsername username) {
    String usernameString = username.getUsername();
    JsonResponse response = new JsonResponse();
    response.setMessage(usernameString + "Available");
    return response;
}

Ajax Function

   function displayUsernamError(data) {
    var json = "<h4>Eroare</h4><pre>"
            + JSON.stringify(data, null, 4) + "</pre>";
    $('#usernameError').html(json);
}

function checkUsername(){
    var username = document.getElementById("username");
    var search = {
            "username" : username.value
    }

    $.ajax({
            type : "POST",
            contentType : 'application/json; charset=utf-8',
            dataType : 'json',
            url : "http://localhost:8080/skill-assessment/register/checkUsername.html",
            data : JSON.stringify(search),
            success : function(result) {
                console.log("SUCCESS: ", data);
                displayUsernamError(result);
            },
            error: function(e){
                console.log("ERROR: ", e);
                displayUsernamError(e);
            },
            done : function(e) {
                console.log("DONE");
            }
    });
}

And finally the html form:

<form:form modelAttribute="user" method="POST" enctype="utf8">
        <tr>
            <td><label>Username</label></td>
            <td><form:input path="name" id="username" /></td>
            <td>
            <p id="usernameError">
            </p>
            </td>
        </tr>
        <button type="button" onClick="checkUsername()">Register</button>
    </form:form>

So, I'm calling the checkUsername method when I press click on the Register button, only for test.

The ajax function is called and this send the JSON to the controller. I used the debug and the controller seems to get the JSON Object.

The problem is with the controller Response, because in the error div from my html page, I get this error:

HTTP Status 406 The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request \"accept\" headers.

Where is the problem? Who can give me an idea?

  • 写回答

4条回答 默认 最新

  • 游.程 2016-02-25 06:38
    关注

    use @ResponseBody need object to json, seems missing some jar file (jackson-mapper-asl.jar and jackson-core-asl.jar) try to import it

    评论

报告相同问题?

悬赏问题

  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助