簡體   English   中英

如何從React Native應用程序向快遞服務器發送請求?

[英]How do you send a request to an express server from a react native app?

我有2個單獨的應用程序,一個react-native應用程序和一個express后端服務器。 我使用express-generator創建了服務器。 我的服務器在users.js具有以下代碼

router.get('/', function(req, res, next) {
  res.json([{
    id: 1,
    username: "foo"
  }, {
    id: 2,
    username: "bar"
  }]);
});

服務器設置為偵聽端口8080並具有app.use('/users', users) 基本上,只有express-generator隨附的所有代碼。

在我的本機應用程序中,我有: "proxy": "http://localhost:8080"

然后在App.js

componentDidMount() {
  fetch('/users'
   .then(res => res.json())
   .then(users => this.setState({ users }))
}

我正在使用Expo將我的應用程序加載到Android設備上,並且得到: unexpected url: /users 如果我在瀏覽器中訪問localhost:8080 ,則數據可見,因此我認為問題必須出在客戶端。

發出請求時,您需要添加完整的主機名和端口號。 在App.js中以http:// localhost:8080 / users獲取數據時,您需要編寫完整的url。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM