https://github.com/vuejs/awesome-vue#http-requests

vue-resource - npm https://www.npmjs.com/package/vue-resource

// global Vue object
Vue.http.get('/someUrl', [config]).then(successCallback, errorCallback);
Vue.http.post('/someUrl', [body], [config]).then(successCallback, errorCallback); // in a Vue instance
this.$http.get('/someUrl', [config]).then(successCallback, errorCallback);
this.$http.post('/someUrl', [body], [config]).then(successCallback, errorCallback); pagekit/vue-resource https://github.com/pagekit/vue-resource/blob/HEAD/docs/http.md 与echo框架整合
package main

import (
"net/http" "github.com/labstack/echo"
"io"
"html/template"
"fmt"
) /*
1.实现 echo.Renderer 接口
*/
type Template struct {
templates *template.Template
} func (t *Template) Render(w io.Writer, name string, data interface{}, c echo.Context) error {
return t.templates.ExecuteTemplate(w, name, data)
} /*
4.在 action 中渲染模板
*/
func Hello(c echo.Context) error {
return c.Render(http.StatusOK, "WeUI", "chkUrl")
} /*
自定义一个 context
Define a custom context
Context - Go/Golang 框架 Echo 文档 http://go-echo.org/guide/context/
*/ type CustomContext struct {
echo.Context
} func (c *CustomContext) Foo() {
println("foo")
} func (c *CustomContext) Bar() {
println("bar")
} type ScriptStruct struct {
Host string
Port int
Path string
ScriptName string
} var s2 = ScriptStruct{"192.168.3.123", 8088, "/myDir/", "spider.go"}
var ScriptArr [2]ScriptStruct func (c *CustomContext) DumpScripts() {
println("bar")
ScriptArr[0] = ScriptStruct{"192.168.3.103", 8088, "/home/goDev/spider/", "spiderChkurl.go"}
ScriptArr[1] = ScriptStruct{"192.168.3.110", 8088, "/home/goDev/spider/", "spiderChkurl.go"}
//ScriptArr[2] = ScriptStruct{"192.168.3.123", 8088, "/home/goDev/spider/", "spiderChkurl.go"}
} // User
type User struct {
Name string `json:"name" xml:"name"`
Email string `json:"email" xml:"email"`
} func main() {
/*
2.预编译模板
*/
t := &Template{
templates: template.Must(template.ParseGlob("goEchopublic/views/*.html")),
} /*
3.注册模板
*/
e := echo.New()
e.Renderer = t /*
静态文件
Echo#Static(prefix, root string) 用一个 url 路径注册一个新的路由来提供静态文件的访问服务。root 为文件根目录。
这样会将所有访问/static/*的请求去访问assets目录。例如,一个访问/static/js/main.js的请求会匹配到assets/js/main.js这个文件。
*/
e.Static("/static", "assets") /*
创建一个中间件来扩展默认的 context
Create a middleware to extend default context
*/ e.Use(func(h echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
cc := &CustomContext{c}
return h(cc)
}
})
/*
这个中间件要在所有其它中间件之前注册到路由上。
This middleware should be registered before any other middleware.
*/ /*
在业务处理中使用
Use in handler
*/
e.GET("/scriptAdmin01", Hello) e.GET("/WeUI", func(c echo.Context) error {
fmt.Println("test-")
return c.Render(http.StatusOK, "WeUI", "data--")
}) e.GET("/scriptAdmin", func(c echo.Context) error {
cc := c.(*CustomContext)
cc.Foo()
cc.Bar()
cc.DumpScripts()
fmt.Println(ScriptArr)
return c.Render(http.StatusOK, "hello", s2)
}) e.GET("/chkUrl", func(c echo.Context) error {
fmt.Println("test-chkUrl")
cc := c.(*CustomContext)
cc.DumpScripts()
return c.Render(http.StatusOK, "chkUrl", ScriptArr)
})
e.POST("/chkUid", func(c echo.Context) error {
fmt.Println("chkUid")
uid := c.FormValue("uid")
fmt.Println(uid)
cc := c.(*CustomContext)
cc.DumpScripts() msg := "uid:" + uid + "处理中。。。进度xxx"
fmt.Println(msg) return c.Render(http.StatusOK, "tmp", msg)
})
e.GET("/testVue", func(c echo.Context) error {
fmt.Println("testVue")
//uid := c.FormValue("uid")
//fmt.Println(uid)
//cc := c.(*CustomContext)
//cc.DumpScripts()
uid := "12"
msg := "uid:" + uid + "处理中。。。进度xxx"
fmt.Println(msg)
u := &User{
Name: "Jon",
Email: "jon@labstack.com",
}
return c.JSON(http.StatusOK, u)
})
e.Logger.Fatal(e.Start(":1323"))
}
{{define "chkUrl"}}
<!DOCTYPE html>
<html lang="zh-cmn-Hans">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=0">
<title>chkUrl--脚本管理</title>
<link rel="stylesheet" href="/static/WeUI_files/weui.css">
<link rel="stylesheet" href="/static/WeUI_files/example.css">
</head>
<body style="margin:1em ;">
<div class="weui-cells page__category-content">
<div class="page__hd">
<h1 class="page__title">chkUrl</h1>
<p class="page__desc">检测url状态信息</p>
</div>
</div>
<div class="weui-cells page__category-content">
{{range .}}
<form action="/chkUid" method="POST">
<div class="weui-cell weui-cell_swiped">
<div class="weui-flex">
<div class="weui-flex__item">
<div class="placeholder">{{.Host}}</div>
</div>
<div class="weui-flex__item">
<div class="placeholder">{{.Port}}</div>
</div>
<div class="weui-flex__item">
<div class="placeholder">{{.Path}}</div>
</div>
<div class="weui-flex__item">
<div class="placeholder">{{.ScriptName}}</div>
</div>
<div class="weui-flex__item">
<div class="placeholder"><input class="weui-input" type="number" pattern="[0-9]*"
value="weui input error"
placeholder="请输入uid(0-9)" style="margin-left: 2em;" name="uid">
</div>
</div>
<div class="weui-flex__item">
<div class="placeholder">
<button class="weui-swiped-btn weui-swiped-btn_warn" href="javascript:"
style="color: #FFF;text-align: center;" type="submit">运行
</button>
</div>
</div>
</div>
</div>
</form>
{{end}}
</div>
<div id="app">
@{message}@
</div>
</body>
</html>
<script src="https://cdn.jsdelivr.net/npm/vue"></script>
<script src="https://cdn.jsdelivr.net/npm/vue-resource@1.5.1"></script>
<script>
var app = new Vue(
{
delimiters: ['@{', '}@'],
el: '#app',
data: {
message: 'Hello Vue!'
}, }
); app.$http.get('/testVue', {foo: 'bar'}).then(response = > { // get status
response.status; // get status text
response.statusText; // get 'Expires' header
response.headers.get('Expires'); // get body data
//注意服务端为“Email”,
// response.body.Email; 数据不被解析
app.message = response.body.email; },
response =
>
{
// error callback
}
) </script>
{{end}}

vue http 请求的更多相关文章

  1. VUE开发请求本地数据的配置,旧版本dev-server.js,新版本webpack.dev.conf.js

    VUE开发请求本地数据的配置,早期的vue-lic下面有dev-server.js和dev-client.js两文件,请求本地数据在dev-server.js里配置,最新的vue-webpack-te ...

  2. Vue 网络请求

    Vue网络请求,用的是vue-resource 1. 首先需要安装vue-resource npm install vue-resource 2. 安装好之后,会在package.json文件中自动加 ...

  3. 手把手教你vue配置请求本地json数据

    本篇文章主要介绍了vue配置请求本地json数据的方法,分享给大家,具体如下:在build文件夹下找到webpack.dev.conf.js文件,在const portfinder = require ...

  4. vue开发请求本地模拟数据的配置方法(转)

    VUE开发请求本地数据的配置,早期的vue-lic下面有dev-server.js和dev-client.js两文件,请求本地数据在dev-server.js里配置,最新的vue-webpack-te ...

  5. vue.js 请求数据

    VUE.JS var vm = new Vue({ el:"#list", data:{ gridData: "", }, mounted: function( ...

  6. vue数据请求

    我是vue菜鸟,第一次用vue做项目,写一些自己的理解,可能有些不正确,欢迎纠正. vue开发环境要配置本地代理服务.把config文件加下的index.js里的dev添加一些内容, dev: { e ...

  7. 02 . 处理axios的三个问题 :设置基路径/axios挂载到vue原型/请求时自动携带token

    //使用API时必须在请求头中使用 Authorization 字段提供 token 令牌 import axios from 'axios' // 处理axios的三个问题 // 处理一:基路径 a ...

  8. vue发送请求---fetch-jsonp

    fetch-jsonp和axios类似,都是第三方插件返送请求,而vue-resource是vue官方提供的请求插件 前两个哪个组件使用就在那里引入,vue-resource直接在vue的main.j ...

  9. vue路由请求 router

    创建一个Router.js文件 // 路由请求//声明一个常量设置路菜单// import Vue from "vue/types/index";import Vue from ' ...

  10. Vue添加请求拦截器

    一.现象 统一处理错误及配置请求信息 二.解决 1.安装 axios  , 命令: npm install axios --save-dev 2.在根目录的config目录下新建文件 axios.js ...

随机推荐

  1. WPF,布局,Menu,MenuItem,DockPanel,Grid,DockPanel.Dock='',ToolBar,Content,Image,Uri

    布局相关: Grid as title Binding Path="", Mode= TwoWay, model should implement IPropertyChanged ...

  2. Yii使用find findAll查找出指定字段的实现方法

    Yii使用find findAll查找出指定字段的实现方法,非常实用的技巧,需要的朋友可以参考下. 用过Yii的朋友都知道,采用如下方法: 查看代码   打印 1 modelName::model() ...

  3. eclipse 五种断点

    1. Line BreakpointLine Breakpoin是最简单的Eclipse断点,只要双击某行代码对应的左侧栏,就对该行设置上断点. 2. WatchpointLine Breakpoin ...

  4. IntelliJ IDEA导入包的顺序调整和按包类型分类(保持和Eclipse一致)

    调整的内容如下: 空行 import java.* 空行 import javax.* 空行 import com.* 空行 import all other imports 空行 import st ...

  5. pt-pmp :pt toolkit

    http://www.cnblogs.com/ivictor/p/6012183.html

  6. UITableView 滚动时使用reloaddata出现 crash'-[__NSCFArray objectAtIndex:]: index (1) beyond bounds (0)' Crash

    例子: - (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)in ...

  7. 【postman】postman测试API报错如下:TypeError: Failed to execute 'fetch' on 'Window': Invalid value 对中文支持不好

    使用postman测试APi的时候,因为系统需要在header部带上登录用户的信息,所以 如下: 然后测试报错如下:TypeError: Failed to execute 'fetch' on 'W ...

  8. Android动画中Interpolator 详解和演示

    遇到一个项目需求,想让动画变得更活泼一点,于是想到了动画属性中的Interpolator,写了基本例子测试一下Android提供给我们现成的加速器的效果: 效果 代码中方法 xml中属性 越来越快 A ...

  9. AngularJS:实现动态添加输入控件功能

    功能要求如下:1.    点击加号可以增加输入框.2.    点击减号可以减少输入框.3.    当输入框只有一个的时候,不能再减少输入框.效果图如下:只有一个输入框有多个输入框 要实现这个功能,可以 ...

  10. Gvim 和 Opencv编译

    好奇看了一下Gvim编译器:在官网上也有介绍,github上有源码,安装在电脑上,感觉需求不大,记那些命令也太多了.然后编译opencv过程中cmake成功了,但是VS下编译报了很多错,准备不搞这些了 ...