Ajax请求(415 Unsupported Media Type)
Unsupported media type-415(不支持的媒体类型)
该错误类型是后台接收参数为json类型的,然而ajax提交的类型不对,如下:
异常代码:
$.ajax({ url: api + "/sendMessage", type: "post", dataType : "json", cache: false, data: { mobile: base.encode(phone), message: base.encode(sms) }, success: function(data) { if(data.ret == "0") { layer.msg('短信已发到您手机', {icon: 1}); } else { countdown=0; layer.msg('发送失败,请稍后重试', {icon: 2}); } }, error: function(data) { countdown=0; layer.msg('发送异常,请稍后重试', {icon: 2}); } });
正确代码:
$.ajax({ url: api + "/sendMessage", type: "post", dataType : "json", contentType: 'application/json', cache: false, data: JSON.stringify({ mobile: base.encode(phone), message: base.encode(sms) }), success: function(data) { if(data.ret == "0") { layer.msg('短信已发到您手机', {icon: 1}); } else { countdown=0; layer.msg('发送失败,请稍后重试', {icon: 2}); } }, error: function(data) { countdown=0; layer.msg('发送异常,请稍后重试', {icon: 2}); } });