angularjs和jquery前端发送以http请求formdata数据

时间:2023-03-09 06:01:42
angularjs和jquery前端发送以http请求formdata数据

formdata是比较常见的前端发送给后端的请求,不仅可以上传数据,而且同时可以上传文件。

jquery使用http请求上传formdata数据的方法:

var formdata = new FormData();
formdata.append('key', 'value');
formdata.append('键', '这边需要是string,不要写json等');
formdata.append('file', $('#file')[0].files[0]);

$.ajax({
url: url,
data: formdata,
type: 'POST',
contentType: false,
processData: false,
success: function (result) {
console.log(result);
},
error: function (err) {
console.log(err);
}
})

注意:这边contentType以及processData需要设置为false

angularjs使用http请求上传formdata数据的方法:

var formData = new FormData();
formData.append('key', 'value');
formData.append('file', new File([fileBlob], 'filename.txt'));
$http({
method: 'POST',
url: url,
data: formData,
headers: {
'Content-Type': undefined
}
});

注意:angularjs这边的Content-Type必须要设置为undefined,才能够正常的发送formdata
---------------------
作者:wuya1994
来源:CSDN
原文:https://blog.csdn.net/wuya1994/article/details/53428345
版权声明:本文为博主原创文章,转载请附上博文链接!