简体   繁体   English

从ajax中的控制器获取对象-Spring MVC

[英]get object from controller in ajax - spring mvc

I want to get object from controller in ajax method, but I always get error. 我想通过ajax方法从控制器获取对象,但是我总是会出错。 This is my controller: 这是我的控制器:

@RequestMapping(value = "/getUpdatableCard",method = RequestMethod.GET)
public @ResponseBody Card getUpdatableCard(@RequestParam("card") long id) {
    Card card = null;
    for(int i = 0;i<cards.size();i++) {
        if(cards.get(i).getId()==id) {
            card = cards.get(i);
        }
    }
    System.out.println(card.getExpression());
    System.out.println(card.getCardType().getName());
    System.out.println(id);

    return card;
}

This is my ajax function: 这是我的ajax函数:

function addAttribute() {
var card = $('#card').val();
$.ajax({
    type:"GET",
    url: contexPath + "/getUpdatableCard",
    data:"card=" + card,
    success:function(data) {
        $("#cardUpdate").modal("show");
        alert(data.id);
    },
    error:function(e) {
        alert('QIRAGI')
    }
});

} }

in the web console: status:406 statusText:not acceptable 在Web控制台中:status:406 statusText:不可接受

please help me. 请帮我。

controller 控制者

@RequestMapping(value = "/getUpdatableCard/{card}",method = RequestMethod.GET)
public @ResponseBody Card getUpdatableCard(@PathVariable long card) {


    return card;  // break point. you confirm "card". 
}

js js

function addAttribute() {
    var card = $('#card').val();
    $.ajax({
        type:"GET",
        url: contexPath + "/getUpdatableCard/" + card,

        success:function(data) {
            $("#cardUpdate").modal("show");
            alert(data.id);
        },
        error:function(e) {
            alert('QIRAGI')
        }
    });
}

406 Not Acceptable 406不可接受

The resource identified by the request is only capable of generating response entities which have content characteristics not acceptable according to the accept headers sent in the request. 由请求标识的资源仅能够生成响应实体,该响应实体具有根据请求中发送的接受标头不可接受的内容特征。

This situation can occur due to many reasons,Either you don't have the correct Jackson library in your classpath, or you haven't used the <mvc:annotation-driven> directive. 由于多种原因可能会发生这种情况,或者您的类路径中没有正确的Jackson库,或者您没有使用<mvc:annotation-driven>指令。

Maybe you are missing some of JAR files also. 也许您也缺少一些JAR文件。

Spring JSON request getting 406 (not Acceptable) Spring JSON请求获取406(不可接受)

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

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