ajax——傳送不同格式的請求資料
阿新 • • 發佈:2018-12-12
1.上傳formData格式資料
var params = new FormData(); params.append("id", "1"); params.append("name", "cd"); $.ajax({ url: "xxx", type: "POST", data: params, processData: false, contentType: false, mineType: "multipart/form-data", success: function(data) { console.log(data); }, error: function(xhr) { console.log(xhr); } });
2.上傳raw body格式的資料
var params = {
id: 1,
name: "cd"
};
$.ajax({
url: "xxx",
type: "PUT",
data: JSON.stringify(params),
contentType: "application/json,charset=utf-8",
success: function(data) {
console.log(data);
},
error: function(xhr) {
console.log(xhr);
}
});