在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/Linux  HTML/ react 用fetch跨域請(qǐng)求,看控制臺(tái),是成功拿到數(shù)據(jù),但是程序里返回的確

react 用fetch跨域請(qǐng)求,看控制臺(tái),是成功拿到數(shù)據(jù),但是程序里返回的確是error

我是用dva框架,用fetch跨域請(qǐng)求。是成功拿到數(shù)據(jù),但是程序里返回的確是error,不知錯(cuò)在哪里,望指教

1.看了下控制臺(tái),是成功拿到數(shù)據(jù)。如圖:

clipboard.png

打印了下返回結(jié)果,返回的確是error,如圖:

clipboard.png

2.代碼如下:

import fetch from 'dva/fetch';

function parseJSON(response) {
  return response.json();
}

function checkStatus(response) {
  if (response.status >= 200 && response.status < 300) {
    return response;
  }

  const error = new Error(response.statusText);
  error.response = response;
  throw error;
}

/**
 * Requests a URL, returning a promise.
 *
 * @param  {string} url       The URL we want to request
 * @param  {object} [options] The options we want to pass to "fetch"
 * @return {object}           An object containing either "data" or "err"
 */
export default function request(url, options,transform) {

const headers= {
    'Accept': 'application/x-www-form-urlencoded',
    'Access-Control-Allow-Origin':'*',
    'Access-Control-Allow-Methods': 'POST, GET, OPTIONS, PUT, DELETE',
    'Access-Control-Allow-Credentials': true
};
const parmas=Object.assign(headers,options,{credentials: 'include'});
  return fetch(url,parmas)
    .then(checkStatus)
    .then(parseJSON)
    .then(data => {
      
      if(transform instanceof Function){
        return transform(data);
      }

      return data;
    })
}

3.發(fā)起請(qǐng)求的地方:

effects: {
    *queryUser ({payload}, {call, put}) {
      const data = yield call(Services.userInfo, parse(payload));
      console.log(data);
      if (data.success) {
      
      }else{
      
      }
        
}
回答
編輯回答
你的瞳

fetch 的 問題 是因?yàn)? 代理配置出錯(cuò)了,proxy要與env同級(jí)

{
  "entry": "src/index.js",
  "env": {
    "development": {
      "extraBabelPlugins": [
        "dva-hmr",
        "transform-runtime",
        ["import",{"libraryName":"antd","style":"css"}]
      ]
    },
    "production": {
      "extraBabelPlugins": [
        "transform-runtime",
        ["import",{"libraryName":"antd","style":"css"}]
      ]
    }

  },
  "proxy": {
        "/baomuWeb": {
          "target": "http://需要代理的域名/",
          "changeOrigin": true
        }

    }
}

fetch 部分只需要加上 (credentials: 'include'允許帶上cookie),其他沒什么特別

const parmas = {
    ...options,
    credentials: 'include'
  };
  return fetch(url,parmas)
    .then(checkStatus)
    .then(parseJSON)
    .then(data => {
      data.success=data.result==='success';
      if(transform instanceof Function){
        return transform(data);
      }

      return data;
    })
    .catch(err => ({ err }));
2017年8月21日 04:04
編輯回答
深記你

開發(fā)環(huán)境需要配置proxy,將跨域的地址代理出去。
eg:

proxy: {
    '/api/**': {
      'target': 'http://192.168.204.37:8080/'
    },
}

生產(chǎn)環(huán)境也需要配置proxy。如nginx的配置,通過pass_proxy代理出去。

location /api {
    pass_proxy http://xxx.xxx.xx.xx/xxx;
}
2018年2月18日 15:29
編輯回答
護(hù)她命
'Access-Control-Allow-Origin':'*',
    'Access-Control-Allow-Methods': 'POST, GET, OPTIONS, PUT, DELETE',
    'Access-Control-Allow-Credentials': true

這些頭是服務(wù)端返回的,不是請(qǐng)求頭,先刪掉這個(gè)在測(cè)試下

2018年6月26日 14:32