Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Vue.js Ajax(vue-resource)

Vue.js Ajax(vue-resource)

To implement asynchronous loading, Vue needs to use the vue-resource library.

Vue.js 2.0 recommends using axios to perform ajax requests.



Get request

The following is a simple Get request example; the request address is a simple txt file:

Example

window

.

onload

=

function

(

)

{

var

vm

=

new

Vue

(

{

el

:

'

#box

'

,

data

:

{

msg

:

'

Hello World!

'

,

}

,

methods

:

{

get

:

function

(

)

{

//

Send Get request

this

.$

http

.

get

(

'

/try/ajax/ajax_info.txt

'

)

.

then

(

function

(

res

)

{

document

.

write

(

res

.

body

)

;

}

,

function

(

)

{

console

.

log

(

'

Request Failure Handling

'

)

;

}

)

;

}

}

}

)

;

}

Try it »

If you need to pass data, you can use the format this.$http.get('get.php',{params : jsonData}); the second parameter jsonData is the data sent to the backend.



this.$http.get('get.php',{params : {a:1,b:2}}).then(function(res){

    document.write(res.body);    

},function(res){

    console.log(res.status);

});

Post request

Post sends data to the backend and requires a third parameter {emulateJSON:true}.

Purpose of emulateJSON: If the Web server cannot handle requests encoded as application/json, you can enable the emulateJSON option.

Example

window

.

onload

=

function

(

)

{

var

vm

=

new

Vue

(

{

el

:

'

#box

'

,

data

:

{

msg

:

'

Hello World!

'

,

}

,

methods

:

{

post

:

function

(

)

{

//

Send Post request

this

.$

http

.

post

(

'

/try/ajax/demo_test_post.php

'

,

{

name

:

"

Runoob Tutorial

"

,

url

:

"

http://www.runoob.com

"

}

,

{

emulateJSON

:

true

}

)

.

then

(

function

(

res

)

{

document

.

write

(

res

.

body

)

;

}

,

function

(

res

)

{

console

.

log

(

res

.

status

)

;

}

)

;

}

}

}

)

;

}

Try it »

The demo_test_post.php code is as follows:



Syntax & API

You can use the global object Vue.http or use this.$http inside a Vue instance to make HTTP requests.

// 基于全局Vue对象使用http

Vue.http.get('/someUrl', [options]).then(successCallback, errorCallback);

Vue.http.post('/someUrl', [body], [options]).then(successCallback, errorCallback);



// Use $http inside a Vue instance

this.$http.get('/someUrl', [options]).then(successCallback, errorCallback);

this.$http.post('/someUrl', [body], [options]).then(successCallback, errorCallback);

vue-resource provides 7 request APIs (REST style):

get(url, [options])

head(url, [options])

delete(url, [options])

jsonp(url, [options])

post(url, [body], [options])

put(url, [body], [options])

patch(url, [body], [options])

Except for jsonp, the API names of the other 6 are standard HTTP methods.

Explanation of options parameters:

Parameters Type Description
url string The target URL of the request
body Object, FormData, string Data sent as the request body
headers Object Headers object sent as request headers
params Object Parameter object sent as URL parameters
method string HTTP method (e.g. GET, POST, ...)
timeout number Request timeout (unit: milliseconds) (0means never timeout)
before function(request) Callback function that modifies the request before it is sent
progress function(event) Callback function ProgressEvent for handling upload progress
credentials boolean Whether credentials should be presented for cross-site requests
emulateHTTP boolean Whether it is necessary to set theX-HTTP-Method-Overrideheader and send PUT, PATCH, and DELETE requests in the traditional POST way.
emulateJSON boolean Set the request body type toapplication/x-www-form-urlencoded

Handle the response object obtained from a request with the following properties and methods:

property Type Description
url string The URL source of the response
body Object, Blob, string Response body data
headers Header Request headers object
ok boolean This value is true when the HTTP response code is between 200 and 299
status number HTTP response code
statusText string HTTP response status
Method Type Description
text() 约定值 Returns the response body as a string
json() 约定值 Returns the response body as a formatted json object
blob() 约定值 Returns the response body as a binary Blob object