ajax——发送不同格式的请求数据

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

 

你可能感兴趣的:(ajax)