reactjs - 如何从外部访问 localhost asp.net core web api
问题描述
我有反应本机应用程序。我想从 asp.net 核心 Web API 获取数据。我想从外部访问本地主机 asp.net 核心 Web API(从反应本机应用程序)
解决方案
您可以使用许多第三方库进行 HTTP 请求,例如 axios,甚至在您想要访问核心 Web API 时获取 XMLHttpRequest。您可以使用以下方法,无论您的端点是什么。
fetch('https://jsonplaceholder.typicode.com/posts/1', {
method: 'GET'
})
.then((response) => response.json())
.then((responseJson) => {
console.log(responseJson);
this.setState({
data: responseJson
})
})
.catch((error) => {
console.error(error);
});
使用 XMLHttpRequest
var xhr = new XMLHttpRequest()
xhr.addEventListener('load', () => {
console.log(xhr.responseText)
})
xhr.open('GET', 'https://dog.ceo/api/breeds/list/all')
xhr.send()
使用 axios
axios.get('/user?ID=12345')
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
})
.finally(function () {
});
推荐阅读
- javascript - × 从 javascript 添加不可点击?
- python - 朴素贝叶斯的工作原理
- php - Laravel - 在 cron 作业中使用“睡眠”命令
- java - Drools 通过 Java API 定时执行规则
- javascript - 使用 Nest 设置两个不同的静态文件夹
- flutter - 约束高度在颤动中的行/列小部件中为无穷大
- android - 生成签名包(.aab)文件时图像文件上的Android Studio gradle错误
- javascript - 如何在漫长的过程中强制浏览器刷新视图
- python - 在 jupyter 笔记本中单击选项卡按钮后未获得自动列表/菜单
- javascript - 在第一个循环完成后在 jquery 中调用其他函数