1. 程式人生 > >Ajax請求(415 Unsupported Media Type)

Ajax請求(415 Unsupported Media Type)

message spa 手機 pos code one 媒體類型 api nco

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}); } });

Ajax請求(415 Unsupported Media Type)