ajax post 请求415\ 400 错误

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zqian1994/article/details/83752211

前后台通过ajax进行数据交互的时候出现了异常报错415,
请求方式如下:

// 错误1...
 $.ajax({
                url: url + '/license/generate',
                type: 'post',
                data: {
                    regCode: '1a57-211a-cb2f-f496-0892-6cc4-8d6f-496e-9cc4-3ae7-64f7-d1ea-2770-bbc5-16',
                    regPer: $('#regPer').val(),
                    exporeTime: $('#exporeTime').val(),
                    salePer: $('#salePer').val(),
                    agentPer: $('#agentPer').val(),
                    maxOrgNum: $('#maxOrgNum').val(),
                    implPer: $('#implPer').val(),
                    memo: $('#memo').val()
                },
                dataType: 'json',
                success: function (res) {
                    console.log(res)
                }
            })

查看控制台 返回 错误415(不支持的媒体类型)

error: "Unsupported Media Type"
exception: "org.springframework.web.HttpMediaTypeNotSupportedException"
message: "Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported"
path: "/license/generate"
status: 415
timestamp: 1541405303823

经过同事提醒,此处post提交时header中的contentType类型异常

application/x-www-form-urlencoded 最常见的 POST 提交数据的方式
multipart/form-data 使用表单上传文件时,传递这个值
application/json 用来告诉服务端消息主体是序列化后的 JSON 字符串
text/xml 使用 HTTP 作为传输协议,XML 作为编码方式的远程调用规范
内容参考:
四种常见的 POST 提交数据方式对应的content-type取值
菜鸟驿站HTTP content-type 对照表

初次修改后进行提交

// 错误2....
$.ajax({
                url: url + '/license/generate',
                type: 'post',
                data: {
                    regCode: '1a57-211a-cb2f-f496-0892-6cc4-8d6f-496e-9cc4-3ae7-64f7-d1ea-2770-bbc5-16',
                    regPer: $('#regPer').val(),
                    exporeTime: $('#exporeTime').val(),
                    salePer: $('#salePer').val(),
                    agentPer: $('#agentPer').val(),
                    maxOrgNum: $('#maxOrgNum').val(),
                    implPer: $('#implPer').val(),
                    memo: $('#memo').val()
                },
                dataType: 'json',
                contentType: 'application/json; charset=UTF-8',// contn
                success: function (res) {
                    console.log(res)
                }
            })

!!!400 无效请求
前后台传入的数据不匹配,后台无法解析数据,进行查询。emmmm…经过一番百度,提到对请求数据进行json 序列

$.ajax({
                url: url + '/license/generate',
                type: 'post',
                // JSON.stringify解决404错误请求
                data: JSON.stringify({ 
                    regCode: '1a57-211a-cb2f-f496-0892-6cc4-8d6f-496e-9cc4-3ae7-64f7-d1ea-2770-bbc5-16',
                    regPer: $('#regPer').val(),
                    exporeTime: $('#exporeTime').val(),
                    salePer: $('#salePer').val(),
                    agentPer: $('#agentPer').val(),
                    maxOrgNum: $('#maxOrgNum').val(),
                    implPer: $('#implPer').val(),
                    memo: $('#memo').val()
                }),
                dataType: 'json',
                contentType: 'application/json; charset=UTF-8',// 解决415错误
                success: function (res) {
                    $('.regcodearea').show().siblings().hide();
                    $('#copyText').val(res.data);
                }
            })

perfect!成功!!!

猜你喜欢

转载自blog.csdn.net/zqian1994/article/details/83752211