ajax post请求报错415或400解决方案

post提交数据四种常见的content-type取值:

1.application/x-www-form-urlencoded :
	最常见的 POST 提交数据的方式
2.multipart/form-data :
	使用表单上传文件时,传递这个值
3.application/json :
	用来告诉服务端发送的数据是序列化后的 JSON 字符串
4.text/xml :
	使用 HTTP 作为传输协议,XML 作为编码方式的远程调用规范

  

<script>
    //页面加载事件
    $(function () {
        $("#btn").click(function () {

            //发送ajax请求
            var url = "user/testAjax";
            var params = {"username":"liangge", "password":"123", "age":18};
            $.ajax({
                url:url,
                data:params,
                type:"post",
                success:function (resultData) {
                    //成功后的回调函数
                    console.log(resultData);
                    console.log(resultData.username);
                    console.log(resultData.password);
                    console.log(resultData.age);
                },
                dataType:"json",
                contentType: 'application/json;charset=UTF-8'
            });
        });
    });
</script>

  

posted @ 2022-06-28 11:32  楼前竹  阅读(87)  评论(0)    收藏  举报