vue http 请求
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 请求的更多相关文章
- VUE开发请求本地数据的配置,旧版本dev-server.js,新版本webpack.dev.conf.js
VUE开发请求本地数据的配置,早期的vue-lic下面有dev-server.js和dev-client.js两文件,请求本地数据在dev-server.js里配置,最新的vue-webpack-te ...
- Vue 网络请求
Vue网络请求,用的是vue-resource 1. 首先需要安装vue-resource npm install vue-resource 2. 安装好之后,会在package.json文件中自动加 ...
- 手把手教你vue配置请求本地json数据
本篇文章主要介绍了vue配置请求本地json数据的方法,分享给大家,具体如下:在build文件夹下找到webpack.dev.conf.js文件,在const portfinder = require ...
- vue开发请求本地模拟数据的配置方法(转)
VUE开发请求本地数据的配置,早期的vue-lic下面有dev-server.js和dev-client.js两文件,请求本地数据在dev-server.js里配置,最新的vue-webpack-te ...
- vue.js 请求数据
VUE.JS var vm = new Vue({ el:"#list", data:{ gridData: "", }, mounted: function( ...
- vue数据请求
我是vue菜鸟,第一次用vue做项目,写一些自己的理解,可能有些不正确,欢迎纠正. vue开发环境要配置本地代理服务.把config文件加下的index.js里的dev添加一些内容, dev: { e ...
- 02 . 处理axios的三个问题 :设置基路径/axios挂载到vue原型/请求时自动携带token
//使用API时必须在请求头中使用 Authorization 字段提供 token 令牌 import axios from 'axios' // 处理axios的三个问题 // 处理一:基路径 a ...
- vue发送请求---fetch-jsonp
fetch-jsonp和axios类似,都是第三方插件返送请求,而vue-resource是vue官方提供的请求插件 前两个哪个组件使用就在那里引入,vue-resource直接在vue的main.j ...
- vue路由请求 router
创建一个Router.js文件 // 路由请求//声明一个常量设置路菜单// import Vue from "vue/types/index";import Vue from ' ...
- Vue添加请求拦截器
一.现象 统一处理错误及配置请求信息 二.解决 1.安装 axios , 命令: npm install axios --save-dev 2.在根目录的config目录下新建文件 axios.js ...
随机推荐
- C# 打印webBrowser打开的页面
this.webBrowser.Navigate(webBrowserUrl, tagerFrameName, postBuffer, heads); this.webBrowser.Document ...
- python re的findall和finditer
记录一个现象: 今天在写程序的时候,发现finditer和findall返回的结果不同.一个为list,一个为iterator. 红色箭头的地方,用finditer写的时候,print(item.gr ...
- Windows Phone 8 适应多屏分辨率
Windows Phone 8 比较 windows phone 7 来说有很多功能及性能上的提升例如支持多核 CUP.支持SD卡.多种分辨率. 显然WP7 WVGA - 480x800 的已经不能完 ...
- [Machine Learning with Python] How to get your data?
Using Pandas Library The simplest way is to read data from .csv files and store it as a data frame o ...
- 推荐一款基于 AI 开发的 IDE 插件,帮助提升编码效率
最近在浏览技术社区的时候,发现了一款神奇 IDE 插件,官网称可以利用 AI 帮助程序员写代码,一下子吸引了我的好奇心.赶紧下载下来使用一番,感觉确实蛮神奇,可以火速提升编程效率. 这款插件叫做 ai ...
- 51 NOD 1407 and and and and !!
首先与等于零 相当于要求 每一位 在选的数里都有至少一个在该位为 0.直接求这个不太好求,我们考虑容斥: 设F(s) 为 不合法的位的集合至少是s的方案数 ,某一位不合法当且仅当选的数在这一位都是 ...
- hdu6223(后缀数组)
题意: 给一个长度为n的字符串s[0..n-1],但i的后继不再是i+1,而是(i*i+1)%n,求所有长度为n的“子串”中,字典序最大的是谁 n<=150000 分析: 如果是一般的字符串,那 ...
- mac 当前位置打开终端
做开发时经常会遇到在当前目录打开终端的情况,一直都是先启动终端,然后再切换到当前目录,今天发现了一个新的方法,虽然不是一步到位,但比以前快多了. 工具/原料 mac系统 苹果电脑 方法/步骤 ...
- 纯CSS3美化radio和checkbox
如题,主要通过CSS3来实现将radio和checkbox美化的效果.可是兼容性并非非常好,PC端仅仅支持chrome浏览器(IE和Firefox測试不行,其它没有很多其它測试).然后微信端和QQ端訪 ...
- Spring 与 MyBatis 整合
一.实验介绍 1.1 实验内容 本节课程将整合 Spring 和 MyBatis,并采用 Junit 进行单元测试. 1.2 实验知识点 Spring - MyBatis 整合 Junit 单元测试 ...