欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Vue resource中的GET與POST請(qǐng)求的實(shí)例代碼

 更新時(shí)間:2017年07月21日 11:18:34   作者:craftmanship  
本篇文章主要介紹了Vue resource中的GET與POST請(qǐng)求的實(shí)例代碼,非常具有實(shí)用價(jià)值,需要的朋友可以參考下

前言:vue-resource 使用比 jQuery 更加簡(jiǎn)潔的方式實(shí)現(xiàn)了異步請(qǐng)求功能,還提供了比如 interceptors 這樣處理請(qǐng)求過程中行為的功能。下面介紹下vue-resource中常用的GET與POST請(qǐng)求使用與封裝方法。

訪問 Github 獲取最新的開發(fā)文件與文檔

特征:

  1. 支持 Promise API 和 URI Templates
  2. 支持請(qǐng)求過程中使用攔截器(interceptoers)
  3. 支持 Firefox,Chrome,Safari,Opera 和 IE9+
  4. 非常的?。▔嚎s后之后14KB,在啟用 gzipped后只有5.3KB)

參數(shù)說明:

參數(shù)說明很多文章里面所已經(jīng)說過了,這里只使用必須用到的參數(shù),具體請(qǐng)?jiān)L問 Github 中的 Document

GET請(qǐng)求

function getRequest(url, params) {
 return new Promise((resolve, reject) => {
  Vue.http.get(
   url,
   {
    params: params
   },
   {emulateJSON: true}
  )
  .then((res) => {
   resolve(res);
  })
  .catch((res) => {
   reject(res);
  });
 });
}

POST請(qǐng)求

function postRequest(url, params) {
 return new Promise((resolve, reject) => {
  Vue.http.post(
   url,
   {
    params
   },
   {emulateJSON: true}
  )
  .then((res) => {
   resolve(res.body);
  })
  .catch((res) => {
   reject(res.body);
  });
 });
}

使用方法

var params = new Object(); //創(chuàng)建params對(duì)象
var params.id = id; //傳遞參數(shù)
var url = url; //url地址
postRequest(url, params)
.then((message) => {
 //這里處理成功回調(diào)
})
.catch((message) => {
 //這里處理失敗回調(diào)
});

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論