• 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
    })


  • 相关阅读:
    A Tour of Go For continued
    A Tour of Go For
    Request对象的主要方法
    JAVA中的异常疑点解析
    用JDBC如何调用存储过程
    字节流与字符流的区别
    js判断第二个日期比第一个日期大
    JAVA中会存在内存泄露吗
    mysql 建库建表建用户
    mysql 的数据类型
  • 原文地址:https://www.cnblogs.com/congxueda/p/7081026.html
Copyright © 2020-2023  润新知