简体   繁体   English

如何使用 Expo 在您的 React Native 中连接到 API?

[英]How to Connect to an API in Your React Native With Expo?

Api Api

Api is made by laravel in my local environment. Api 是由 laravel 在我的本地环境中制造的。

ApiController ApiController

public function test(Request $request)
{
    $test = Test::all();
    return response()->json($test, 200);
}

When I get http://127.0.0.1:8000/api/test , it works.当我得到http://127.0.0.1:8000/api/test时,它可以工作。


React Native With Expo使用 Expo 反应原生

When I did expo start , this project has started on http://localhost:19002/当我做expo start ,这个项目已经开始在 http://localhost:19002/

App.js应用程序.js

componentDidMount() {
  axios
    .get('http://127.0.0.1:8000/api/test')
      .then((response) => {
        console.log(response);
      })
      .catch((error) => {
        console.error(error);
      });
}

I got an error:(我收到一个错误:(

Network Error
- node_modules/axios/lib/core/createError.js:15:17 in createError
- node_modules/axios/lib/adapters/xhr.js:80: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

I check this project on Expo Client App on iPhone.我在 iPhone 上的 Expo 客户端应用程序上检查了这个项目。

I would appreciate it if you could give me any advice:)如果您能给我任何建议,我将不胜感激:)

Expo doc say: Expo 文档说:

By default, iOS will block any request that's not encrypted using SSL.默认情况下,iOS 将阻止任何未使用 SSL 加密的请求。 If you need to fetch from a cleartext URL (one that begins with http) you will first need to add an App Transport Security exception.如果您需要从明文 URL(以 http 开头)中获取,您首先需要添加应用程序传输安全异常。 If you know ahead of time what domains you will need access to, it is more secure to add exceptions only for those domains;如果您提前知道需要访问哪些域,则只为这些域添加例外会更安全; if the domains are not known until runtime you can disable ATS completely.如果直到运行时域才知道,您可以完全禁用 ATS。 Note however that from January 2017, Apple's App Store review will require reasonable justification for disabling ATS.但请注意,从 2017 年 1 月起,Apple 的 App Store 审核将需要合理的理由来禁用 ATS。 See Apple's documentation for more information.有关详细信息,请参阅 Apple 的文档。

https://docs.expo.io/versions/v37.0.0/react-native/network/#using-fetch https://docs.expo.io/versions/v37.0.0/react-native/network/#using-fetch

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM