【问题标题】:React js fetch api show different behaviourReact js fetch api显示不同的行为
【发布时间】:2020-03-04 18:44:11
【问题描述】:

我使用 fetch API 进行服务器调用,除了一个 post 请求之外,所有另一个 post/get 请求都命中了确切/desire 服务器 URL,但一个 post 请求命中了 localhost 服务器。 p>

发布请求的服务类代码和基本 URL 与其他相同。

service.js:56 POST http://localhost:3000/.... 500(内部服务器错误)

代码:

function save(workflow) {
    const token = authenticationGenerator.generateAuthenticationToken(localStorage.getItem('username'),
        localStorage.getItem('password'));

    const requestOptions = {
        method: 'POST',
        headers: { 'Content-Type': 'application/json', 'Authorization': token },
        body: JSON.stringify(workflow)
    };
    return fetch('/workflow', requestOptions)
        .then(handleResponse).then(workflowData => {
            return workflowData;
        });
}

...点击本地主机...

function save(phase) {
    const token = authenticationGenerator.generateAuthenticationToken(localStorage.getItem('username'),
        localStorage.getItem('password'));
    const requestOptions = {
        method: 'POST',
        headers: { 'Content-Type': 'application/json', 'Authorization': token },
        body: JSON.stringify(phase)
    };
    return fetch('/phases', requestOptions)
        .then(handleResponse).then(phaseData => {
            return phaseData;
        });
}

...package.json ...

"scripts": {
    "start": "react-scripts start",
    "build": "webpack --mode production",
    "test": "react-scripts test --env=jsdom",
    "eject": "react-scripts eject"
},
"proxy": "http://url../v1",
"eslintConfig": {
    "extends": "react-app"
},

【问题讨论】:

  • 如果您在设备上进行测试,请确保您使用的是同一个 WiFi
  • 您是否尝试过postman 以确保您的/phases 端点工作正常?代码 500 是服务器的一部分
  • 是的,在邮递员上它工作得很好。点击这里:pasteboard.co/IFHBlfk.png

标签: javascript reactjs react-native react-router


【解决方案1】:

服务器端/phases 位置的路由是否正确?

【讨论】:

  • 那些函数名称相同,参数数量相同,你确定它们没有在调用端混淆吗?我的意思是,你怎么知道哪个被调用了?
  • URL 端点和 requestionOption 方法不同 ... baseurl/phase 和 baseurl/workflow
猜你喜欢
  • 1970-01-01
  • 2018-12-11
  • 1970-01-01
  • 2022-11-22
  • 2018-06-23
  • 1970-01-01
  • 2022-11-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多