将jQuery代码转换为axios

时间:2019-01-09 11:43:34

标签: jquery ajax reactjs axios

我有一些使用jQuery将JSON字符串中的PUT请求提交到PGHP后端的代码。我想在Axios中做同样的事情。我尝试使用代码中列出的Axios,但服务器没有任何帮助。

let signupInfo = {};
signupInfo.account = {};
signupInfo.account = {};
signupInfo.account.type = 'affiliate';
signupInfo.user = {};
signupInfo.user.userName = 'some_name';
signupInfo.site = {};
signupInfo.siteId = 'aqterm';
signupInfo.request = 'availability';
signupInfo.type = 'affiliate';
signupInfo.mode = 'affiliate';

let packagedSignUpInfoData = base64encode(JSON.stringify(signupInfo));

const thePackageObject = {
  "params": {
    "data": packagedSignUpInfoData
  }
};

let packagedData = JSON.stringify(thePackageObject);
let url_request = "https://somedomain.com/aqmprocess";

$.ajax({
  url: "https://somedomain.com/aqmprocess",
  type: 'put',
  data: packagedData,
  dataType: "json",
  cache: false,
  error: function(response) {
    console.log(response);
  },
  success: function(response) {
    if (response.successful === true) {
      console.log(response);
    } else if (response.successful === false) { // we have a form error
      console.log(response);
    }
  }
});

这是axios版本:

const options = {
  method: 'PUT',
  headers: {
    'content-type': 'application/json'
  },
  data: packagedData,
  "https://somedomain.com/aqmprocess",
};

let promise = axios(options).then(function(response) {
  if (response.success === true) {
    return response.data.token;
  }
  console.log(response);
}).catch(function(response) {
  console.log(response);
});

结果将返回一个对象

{ "successful": true, "message": "available" }

0 个答案:

没有答案