如何在 Expo 上使用全局变量调用 API_URL
how to call API_URL using global variable on Expo
我正在尝试使用 Expo 示例中的 IP 地址拨打电话 api,例如 React Native 上的登录功能
UserLoginFunction = () =>
{
const {UserEmail} = this.state;
const {Password} = this.state;
if (!UserEmail.trim()) {
this.setState ({showError: this.state.UserEmail === ""})
return;
}
if (!Password.trim()) {
this.setState ({showError: this.state.Password === ""})
return;
}
var api = "http://192.168.43.123/Api/login.php?email=" + UserEmail + "&password=" + Password;
console.log(api);
return fetch(api)
.then((response) => response.json())
.then((responseJson) => {
/*console.log(responseJson.message);*/
if(responseJson.status === true)
{
console.log(UserEmail);
this.props.navigation.navigate('HomeScreen', { UserEmail : UserEmail });
}
else
{
alert(responseJson.message)
}
}).catch((error) => {
console.error(error);
});
}
我想为像我的 PHP
这样的 IP 地址创建全局变量
public function linkUrl()
{
$value = "http://192.168.43.123/";
return $value;
}
可能吗?我正在寻找它以提高效率,因此只有 1 个 IP 地址可以使用 declare API_URL 或 URL 进行调用
有什么建议吗?谢谢
您需要手动设置BASE_URL。
//config.js
export const BASE_URL = "http://192.168.43.123";
//in your js
import * as config from 'config.js';
...
fetch( config.BASE_URL+ '/path/to/your/api' )
...
我还建议您使用 axios 而不是 fetch。然后可以全局设置base_url
我正在尝试使用 Expo 示例中的 IP 地址拨打电话 api,例如 React Native 上的登录功能
UserLoginFunction = () =>
{
const {UserEmail} = this.state;
const {Password} = this.state;
if (!UserEmail.trim()) {
this.setState ({showError: this.state.UserEmail === ""})
return;
}
if (!Password.trim()) {
this.setState ({showError: this.state.Password === ""})
return;
}
var api = "http://192.168.43.123/Api/login.php?email=" + UserEmail + "&password=" + Password;
console.log(api);
return fetch(api)
.then((response) => response.json())
.then((responseJson) => {
/*console.log(responseJson.message);*/
if(responseJson.status === true)
{
console.log(UserEmail);
this.props.navigation.navigate('HomeScreen', { UserEmail : UserEmail });
}
else
{
alert(responseJson.message)
}
}).catch((error) => {
console.error(error);
});
}
我想为像我的 PHP
这样的 IP 地址创建全局变量public function linkUrl()
{
$value = "http://192.168.43.123/";
return $value;
}
可能吗?我正在寻找它以提高效率,因此只有 1 个 IP 地址可以使用 declare API_URL 或 URL 进行调用 有什么建议吗?谢谢
您需要手动设置BASE_URL。
//config.js
export const BASE_URL = "http://192.168.43.123";
//in your js
import * as config from 'config.js';
...
fetch( config.BASE_URL+ '/path/to/your/api' )
...
我还建议您使用 axios 而不是 fetch。然后可以全局设置base_url