鍍金池/ 問答/Linux  HTML/ unsupported bodyinit type怎么解決

unsupported bodyinit type怎么解決

let getUrl = (url, body) => {
  var b = '';
  for (var bo in body) {
    b += `&${bo}=${body[bo]}`;
  }
  if (b.substr(0, 1) == '&') b = b.substr(1);
  if (url.indexOf('?') === -1) {
    return `${url}?$`;
  }
  return `${url}&$`;
};

export const request = (url, method, body, isUrlParam) => {
  let isOk;
  return new Promise((resolve, reject) => {
    var opt = {
      method,
      headers: {
        'Content-Type': 'application/json;charset=utf-8'
        //'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8'
      }
    }
    if (isUrlParam)
      url = getUrl(url, body);
    else
      opt.body = body;
    alert(JSON.stringify(opt));
    fetch(url, opt).then((response) => {
      if (response.ok) {
        isOk = true;
      } else {
        isOk = false;
      }
      return response.json();
    }).then((responseData) => {
      if (isOk) {
        resolve(responseData);
      } else {
        reject(responseData);
      }
    }).catch((error) => {
      reject(error);
    });
  });
};

使用fetchbody傳參是報這個錯誤,非要我把參數(shù)放在url中,
要怎么處理這個問題呢?

回答
編輯回答
陌離殤

已經(jīng)解決,方案如下:
請求頭設(shè)置:

'Accept': 'application/json',
'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8'

并且把受到的JSON參數(shù)拼接到url中:

let getUrl = (url, body, isForm) => {
  var b = '';
  for (var bo in body) {
    b += `&${bo}=${body[bo]}`;
  }
  if (b.substr(0, 1) == '&') b = b.substr(1);
  if (isForm) {
    return b;
  }
  if (url.indexOf('?') === -1) {
    return `${url}?$`;
  }
  return `${url}&$`;
};

完整的代碼:

let getUrl = (url, body, isForm) => {
  var b = '';
  for (var bo in body) {
    b += `&${bo}=${body[bo]}`;
  }
  if (b.substr(0, 1) == '&') b = b.substr(1);
  if (isForm) {
    return b;
  }
  if (url.indexOf('?') === -1) {
    return `${url}?$`;
  }
  return `${url}&$`;
};

export const request = (url, method, body, isUrlParam, notice, popNoticeSuccess = false) => {
  let isOk;
  return new Promise((resolve, reject) => {
    var opt = {
      method,
      headers: {
        'Accept': 'application/json',
        //'Content-Type': 'application/json;charset=utf-8'
        'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8'
      }
    }
    if (isUrlParam)
      url = getUrl(url, body, false);
    else
      opt.body = getUrl(url, body, true);
      //alert(JSON.stringify(opt));
    fetch(url, opt).then((response) => {
      if (response.ok) {
        isOk = true;
      } else {
        isOk = false;
      }
      return response.json();
    }).then((responseData) => {
      if (isOk) {
        if (responseData.code == 1) {
          if (popNoticeSuccess) {
            notice('suc', responseData.msg);
          }
          //console.log(JSON.stringify(responseData));
          resolve(responseData.data);
        } else {
          notice('err', responseData.msg);
          reject(responseData.data);
        }
      } else {
        reject(responseData.data);
      }
    }).catch((error) => {
      reject(error);
    });
  });
};
2018年8月2日 03:00