calamus
calamus
冬天从这里夺去的,春天会交还与你。

jquery ajax基本形式:

    $.ajax({
        type: 'POST/get',
        url: '',
        data: {"n":n,"m":m},
        dataType: 'json',
        success: function (parameter) {
// do some thing
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
//do some thing to feedback error
              $("#id").html('there is something wrong!');
                alert(XMLHttpRequest.status);
                alert(XMLHttpRequest.readyState);
                alert(textStatus);
  
        }
    })

最近做一个项目需要ajax,前台传值给后台,后台进行一些操作,因为一些原因后台没有办法收到数值,才发现原来是contenttype 的原因。

contenttype的默认值是application/x-www-form-urlencoded,我写成了application/json,导致没有办法接收数据。

application/json是后台返回的mime类型,不是前台提交的方式。

客户端提交的contentType只能为application/x-www-form-urlencoded 或者 multipart/form-data

我们后台用的Java,发现如果contenttype为application/json的话,后台写成这样就可以了。

@RequestMapping(value = "/sk/api/params", method = RequestMethod.POST)
public void params(@RequestBody Map<String, String> params, HttpServletRequest request, HttpServletResponse response) {

System.out.println("总共获取到:"+params.size()+"个参数");
for(String key : params.keySet()){
System.out.println(key + " : " + params.get(key));
}
writeJson(response, "ok");
} 

 

posted on 2016-08-18 17:26  calamus  阅读(348)  评论(0编辑  收藏  举报