默认情况下,$。ajax()将转换
data为查询字符串(如果还不是字符串),因为
data这里是一个对象,请将
data其更改为字符串,然后设置
processdata:false,这样它就不会转换为查询字符串。
$.ajax({ url: "http://www.example.com/api", beforeSend: function(xhr) { xhr.setRequestHeader("Authorization", "Basic " + btoa("username:password")); }, type: 'POST', dataType: 'json', contentType: 'application/json', processdata: false, data: '{"foo":"bar"}', success: function (data) { alert(JSON.stringify(data)); }, error: function(){ alert("Cannot get data"); }});


