rest - 无法使用 Vue.js 完成简单的 GET 请求
问题描述
为了简单起见,我想以与使用 CURL 或 Postman 的 GET 相同的方式发出请求。例如:
curl https://www.google.com
给
<!doctype html><html itemscope="" ... </body></html>
但是,我无法使用 fetch、axios、request、nee 来做到这一点......
我要做的就是在 Vue.js中进行 GET 调用(到https://www.google.comalert(...);
)并弹出结果。
我怎样才能完成如此简单而基本的任务?
解决方案
你可以调用fetch
一个方法。
Vue.config.devtools = false;
Vue.config.productionTip = false;
var app = new Vue({
el: '#app',
data: {
res: ""
},
methods: {
async get() {
const url = 'https://jsonplaceholder.typicode.com/todos/1';
this.res = await fetch(url).then(r => r.json());
}
}
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
<button @click="get">Fetch</button>
{{ res }}
</div>
推荐阅读
- php - 自动完成引导 php mysql
- firebase - firebase_messaging 如何清除通知?
- node.js - Node.js PUT API 请求在服务器重新启动时第一次失败。其余时间工作
- ruby-on-rails - 如何在“术语”查询中转义特殊符号
- swift - 我如何在 swift 5 上使用推送视图控制器?
- java - JavaFX - 使用字符输入定位图像
- vb.net - 如何让 CType/DirectCast 使用定义的变量而不是直接类型?
- javascript - 表单数据在 Node/Express API 中变得未定义
- php - 在页面模板中调用帖子标题的第一个单词
- ruby-on-rails - 如何在 Rails 模型中获取当前用户