When uploading pictures, you need to provide the user's login token and the pictures to be uploaded. But how can two different data types be posted to the server together?
mui.init();
function fsubmit(){
var data = new FormData(mui('#uploadForm')[0]); //获取图片
$.ajax({
url: 'http://192.168.1.8/api/user-center/avatar',
type: 'POST',
data: {
key:localStorage.getItem('key'), //获取本地的登录令牌
avatar:data //图片
},
cache: false,
processData: false,
contentType: false ,
success:function(data){
console.log(data.datas.testURL);
},
error:function(xhr,type,error){
console.log(xhr.status+xhr.responseText);
//一直返回401,没有权限
}
});
return false;
}
Change the data type of post to formdata, and then load the object in formdata. The following is an example:
You have already created the new FormData, so don’t save objects by yourself. Just use the new one...
Then the backend is slightly adjusted to be able to receive FormData.
Thanks for the invitation:
Token can be placed in headers, and the backend checks the token separately, while this interface only processes images