Axios的Post请求传参的两种方式

Axios的Post请求传参的两种方式

表单 Form Data:

Axios的Post请求传参的两种方式_第1张图片

export const add = (article) => {
  return request({
    url: '/api/blade-yuyihui/article/submit',
    method: 'post',
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },

    data: {
      'articleTitle': article.articleTitle,
      'articleContent': article.articleContent,
      'id':article.id || 0
    },
    transformRequest: [function (data) {
      // Do whatever you want to transform the data
      let ret = ''
      for (let it in data) {
        ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
      }
      return ret
    }],
  })
}

json字符串 Request Payload

Axios的Post请求传参的两种方式_第2张图片

return request({
    headers: {
      'Content-Type': 'application/json'
    },
    transformRequest: [function(data) {
      data = JSON.stringify(data)
      return data
    }],
    url: '/test/add',
    method: 'post',
    params: {},
    data: {
      'name': params.name,                      //传的参数
      'jobId': params.jobId,
      'department': params.department,
      'phone': params.phone,
      'position ': params.position,
      'permis': params.permis,
      'entryTime': params.entryTime
    }
  })

原文链接:https://blog.csdn.net/u013253924/article/details/81772820

你可能感兴趣的:(js,前端,ajax)