helloxielan 2015-06-09 19:01 采纳率: 0%
浏览 21

Spring MVC和Ajax错误406

I'm trying to run an ajax method with spring MVC, but I'm getting the error 406: "The resource identified by this request is only capable of generating responses with characteristics not acceptable According to the request" accept "headers"

Controller:

@Transactional
@Controller("user")
@SessionAttributes("user")
public class HomeController {
@Autowired
private UserDAO daoUser;
@Autowired
private EnterpriseDAO daoEnterprise;
@Autowired
private FuncDAO daoFunc;
@Autowired
private LastPeriodDAO daoLastPeriod;

@RequestMapping("/")
public String index() {
    return "redirect:menu";
}

@RequestMapping(value = "/menu", method = RequestMethod.GET)
public ModelAndView menu(@ModelAttribute("user") User user, Enterprise enterprise) {
    ModelAndView mav = new ModelAndView("user/menu");

    Func func = daoFunc.getFunc(user);
    mav.addObject("func", func);
    mav.addObject("enterprise", enterprise);
    mav.addObject("enterpriseList", daoEmpresa.listEnterprise(func));

    return mav;

}
@RequestMapping(value = "/dynamicMenu", method = RequestMethod.POST)
public @ResponseBody List<LastPeriod> dynamicOption(@ModelAttribute("enterprise") Enterprise enterprise) {

    System.out.println(enterprise.getCnpj());
    List<LastPeriod> options = daoLastPeriod.getLastPeriod(enterprise);

    System.out.println(options.size());

    return options;
}

Request ajax:

$(document).ready(function() {
function enterpriseSelectChange() {
    var enterprise= $(this).serialize();

    $.ajax({
        type: 'POST',
        url: 'dynamicMenu',
        data: enterprise,
    })

    .done(function(data) {
        console.log("success");
        console.log(data)
    })
    .fail(function() {
        console.log("error");
    });
}
$("#cnpj").change(enterpriseSelectChange);

});

Form:

                <form:form modelAttribute="enterprise" commandName="enterprise" class="form-horizontal" method="POST">
                <fieldset>
                    <legend>Olá, ${func.name}</legend>

                    <!-- Select enterprise -->
                    <div class="form-group">
                        <label for="enterprise">Enterprise</label>

                        <form:select path="cnpj" class="form-control">
                            <form:option value="0" label=" Select"/>
                            <form:options items="${enterpriseList}" itemValue="cnpj"/>
                        </form:select>
                    </div>
                </form:form>

Please, anyone have any solutions ?

EDIT

Included controller and form

  • 写回答

4条回答 默认 最新

  • weixin_33720078 2015-06-09 19:19
    关注

    406 means "not acceptable". Try to add header content-type when sending the request:

    $.ajax({
        type: 'POST',
        headers:{
            'Content-type:application/x-www-form-urlencoded'
        },
        url: 'dynamicMenu',
        data: enterprise,
    })
    
    .done(function(data) {
        console.log("success");
        console.log(data)
    })
    .fail(function() {
        console.log("error");
    });
    

    application/x-www-form-urlencoded

    评论

报告相同问题?

悬赏问题

  • ¥15 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误
  • ¥30 最小化遗憾贪心算法上界
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝