

[Vue.js] AJAX XMLHttpRequest (XHR) Example
source link: http://siongui.github.io/2017/03/22/vuejs-ajax-xmlhttprequest-xhr-example/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

[Vue.js] AJAX XMLHttpRequest (XHR) Example
March 22, 2017
Example for XMLHttpRequest (XHR) by Vue.js, which helps you transfer data between a client and a server. This is usually used to retrieve data from a URL, and update only part of the webpage without full refresh.
The following demo make HTTP GET XHR request to the given URL.
This demo can also make cross-domain requests if the server supports CORS, which has the Access-Control-Allow-Origin header set properly. You can try the following cross-domain URL in the demo:
https://golden-operator-130720.appspot.com/sukhada.json
Source Code and Explanation
HTML:
<div id="vueapp"> URL: <input type="text" v-model.trim="url" v-on:keyup.enter="xhr"> <button v-on:click="xhr">XHR</button><br> <pre>{{ info }}</pre> </div> <script src="https://unpkg.com/[email protected]/dist/vue.js"></script>
If the user press the button or enter key in the input element, use the value of the input element as URL and make HTTP GET request by xhr method.
JavaScript:
'use strict'; new Vue({ el: '#vueapp', data: { url: "https://siongui.github.io/xemaauj9k5qn34x88m4h/sukhada.json", info: "" }, methods: { xhr: function() { this.info = "Requesting ..."; var rq = new XMLHttpRequest(); rq.onreadystatechange = function(vm) { if (this.readyState === XMLHttpRequest.DONE) { if (this.status === 200) { vm.info = this.responseText; } else { vm.info = "Request Failed"; } } }.bind(rq, this); rq.open("GET", this.url); rq.send(); } } })
The implementation of xhr method is the same as the code in other tutorials, except that we use bind method to assign the value of this and pass the Vue instance to the onreadystatechange event handler.
If you web page is served via HTTPS, the server that returns data also needs to serve via HTTPS. Otherwise browsers will block the request and make the request fail.
Tested on:
- Chromium Version 56.0.2924.76 Built on Ubuntu , running on Ubuntu 16.10 (64-bit)
- Vue.js 2.2.4
References:
Recommend
-
10
Encapsulating Ajax XMLHTTPRequest Calls within JavaScript classes Tuesday, January 31, 2006 The guys over at Fold [posted][1] an article on how to use multiple XMLHTTPRequest (XHR) calls within a single page. This is somethi...
-
8
ajax中断请求 - XMLHttpRequest.abort()方法终止请求更新日期: 2022-01-26阅读量: 41标签: 请求...
-
7
Not FoundYou just hit a route that doesn't exist... the sadness.LoginRadius empowers businesses to deliver a delightful customer experience and win customer trust. Using the LoginRadius Identity...
-
12
BackAjax Battle: XMLHttpRequest vs the Fetch APIApril 16th, 2022 · 5 min read
-
12
Ajax 之战:XMLHttpRequest与Fetch API比较 Ajax 是大多数 Web 应用程序背后的核心技术。它允许页面向 Web 服务发出进一步的请求,因此无需页面刷新往返服务器即可呈现数据。Ajax一词不是一种技术。相反,它指的是可以从客户端脚本加载服务...
-
6
[Go WebAssembly] XMLHttpRequest (XHR) October 19, 2018 This post shows how to...
-
6
GopherJS XMLHttpRequest (XHR) and MakeFunc Example February 18, 2016...
-
21
This post shows how to interact with servers from browsers. Send data to URL or retrieve data from URL from your browser via XMLHttpRequest method in Go/GopherJS. The full code exam...
-
16
Introduction It is really cool to run Go code in the browser. GopherJS is a compiler from Go to
-
9
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK