zoukankan      html  css  js  c++  java
  • vue vue-resource网络请求

    在使用get/post 网络请求,需要下载插件 "vue-resource"

    npm install vue-resource -s

    在路由要导入及注册

    import Vue from 'vue'
    import Router from 'vue-router'
    import VueResource from 'vue-resource'
    //import Hello from '@/components/Hello'
    
    Vue.use(Router)
    Vue.use(VueResource)
    // 基于全局Vue对象使用http
    Vue.http.get('/someUrl', [options]).then(successCallback, errorCallback);
    Vue.http.post('/someUrl', [body], [options]).then(successCallback, errorCallback);


    // 在一个Vue实例内使用$http
    this.$http.get('/someUrl', [options]).then(successCallback, errorCallback);
    this.$http.post('/someUrl', [body], [options]).then(successCallback, errorCallback);
    
    

    emulateHTTP的作用

    如果Web服务器无法处理PUT, PATCH和DELETE这种REST风格的请求,你可以启用enulateHTTP现象。启用该选项后,请求会以普通的POST方法发出,并且HTTP头信息的X-HTTP-Method-Override属性会设置为实际的HTTP方法。

    Vue.http.options.emulateHTTP = true;

    emulateJSON的作用

    如果Web服务器无法处理编码为application/json的请求,你可以启用emulateJSON选项。启用该选项后,请求会以application/x-www-form-urlencoded作为MIME type,就像普通的HTML表单一样。

    Vue.http.options.emulateJSON = true;

    GET请求
    this.$http.get("url", {params: objArg})
    .then(function(res){
    // success
    },function(){
    //error
    })

    POST请求
    this.$http.post('url', {params}, {emulateJSON: true})
    .then(function(res){
    //success
    },function(){
    //error
    })


  • 相关阅读:
    Spring格式化注解
    Mysql 慢查询和慢查询日志分析
    jQuery中的end()方法
    webService学习笔记
    让Hibernate和触发器协同工作
    JavaScript中的setInterval用法
    jQuery中事情的动态绑定
    mongoDB如何处理多对多关系
    新生儿信息管理系统在线帮助
    MySQL数据库安装与配置详解
  • 原文地址:https://www.cnblogs.com/congxueda/p/7081026.html
Copyright © 2011-2022 走看看