首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Axios无法访问Heroku应用程序-错误:网络错误

Axios无法访问Heroku应用程序-错误:网络错误
EN

Stack Overflow用户
提问于 2020-09-06 19:06:08
回答 1查看 526关注 0票数 0

我使用Express js作为服务器,并部署到Heroku,客户端使用使用Axios的React native (Expo)网络,我的问题是当我使用Postman和本地浏览器请求api时,它工作得很好,但当我使用axios请求api时,显示Network Error

代码语言:javascript
复制
Network Error
- node_modules/axios/lib/core/createError.js:15:17 in createError
- node_modules/axios/lib/adapters/xhr.js:88:22 in handleError
- node_modules/event-target-shim/dist/event-target-shim.js:818:20 in EventTarget.prototype.dispatchEvent
- node_modules/react-native/Libraries/Network/XMLHttpRequest.js:575:10 in setReadyState
- node_modules/react-native/Libraries/Network/XMLHttpRequest.js:389:6 in __didCompleteResponse
- node_modules/react-native/Libraries/vendor/emitter/EventEmitter.js:189:10 in emit
- node_modules/react-native/Libraries/BatchedBridge/MessageQueue.js:425:19 in __callFunction
- node_modules/react-native/Libraries/BatchedBridge/MessageQueue.js:112:6 in __guard$argument_0
- node_modules/react-native/Libraries/BatchedBridge/MessageQueue.js:373:10 in __guard
- node_modules/react-native/Libraries/BatchedBridge/MessageQueue.js:111:4 in callFunctionReturnFlushedQueue
* [native code]:null in callFunctionReturnFlushedQueue

这是my react原生请求api

代码语言:javascript
复制
useEffect(() => {
    axios.get('http://myapp.herokuapp.com/member-test', {
      headers:{  
        "Content-Type": "application/json",
        'Accept': 'application/json',
      },
    })
      .then(res => {
        alert(res)
        const categories = res.data;
        console.log("Sukses");
      })
      .catch(function (response) {
        console.log(response)
        alert(response.message)
      })
  }, []);

和我的服务器端

代码语言:javascript
复制
const app = express();
const PORT = process.env.PORT || 3000
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
var corsOptions ={    
  origin : true,  
  credentials: true,
  exposedHeaders: ['Content-Length', 'X-Foo', 'X-Bar'],
  optionsSuccessStatus : 200
}
app.use(cors({corsOptions}));
app.get('/member-test', (req, res) => {
  res.send('Api Live');
});
app.listen(PORT, () => {
  console.log(`Listening on port ${PORT}`);
});

我不知道问题出在哪里,这几天我被困住了,我真的需要帮助来解决这个问题

EN

回答 1

Stack Overflow用户

发布于 2021-07-18 04:53:07

您正在将corsOptions作为对象传递给cors中间件,请尝试更改此行

代码语言:javascript
复制
app.use(cors({corsOptions}));

在这里

代码语言:javascript
复制
app.use(cors(corsOptions));

希望它能对你有所帮助。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63763424

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档