Go web ajax project
这个是本人学习之用,比较乱
我也不知道这是在教大家什么,但如果你能看懂,那你就能真正干活了
//https-ajax.go
package main
import (
"fmt"
"io"
"net/http"
"encoding/json"
"github.com/bitly/go-simplejson"
)
func OnAjax(res http.ResponseWriter, req *http.Request) {
query := req.URL.Query()
fmt.Println("get ", len(query))
if len(query["param1"]) > 0 {
fmt.Println("get ", query["param1"][0])
}
if len(query["param2"]) > 0 {
fmt.Println("get ", query["param2"][0])
}
var detail MyData
detail.Name = "1"
detail.Other = 2.0789
body, err := json.Marshal(detail)
if err != nil {
panic(err.Error())
}
fmt.Println("body ", body)
fmt.Println("string body ", string(body))
js, err := simplejson.NewJson(body)
if err != nil {
panic(err.Error())
}
fmt.Printf("%T:%v\n",js,js)
_map,_ := js.Map()
fmt.Println("map ", _map)
fmt.Println("js count ", len(_map))
item := js.Get("item")
fmt.Println("item ", item.MustString())
amount := js.Get("amount")
//fmt.Println("amount ", amount.MustBool())
fmt.Println("amount ", amount.MustFloat64())
//io.WriteString(res, "These are data from server")
io.WriteString(res, string(body))
}
func OnRes(res http.ResponseWriter, req *http.Request) {
req.ParseForm()
fmt.Println("post", req.PostForm)
fmt.Println("post param1 ", req.PostFormValue("param1"))
fmt.Println("post param2 ", req.PostFormValue("param2"))
io.WriteString(res, "infosec")
}
type MyData struct {
Name string `json:"item"`
Other float32 `json:"amount"`
}
func main() {
//static web
http.Handle("/", http.FileServer(http.Dir("web")))
//dynamic web
http.HandleFunc("/ajax", OnAjax)
http.HandleFunc("/infosec", OnRes)
// start server
fmt.Println("Server is running at localhost:8086")
//err := http.ListenAndServe(":8086", nil)
err := http.ListenAndServeTLS(":8086", "server.crt", "server.key", nil)
if err != nil {
fmt.Println("Server failure /// ", err)
}
}
//html: /web/ajax.html
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<title>Go语言与ajax示例</title>
</head>
<body>
<p><input id="btn1" type="button" value="按钮" /></p>
<p><input id="txt1" type="text" /></p>
<script>
window.onload = main;
function main() {
var oBtn = document.getElementById('btn1');
oBtn.onclick = OnButton1;
}
var MyXMLHttpRequest = function(){
var xmlhttprequest = new XMLHttpRequest();
if(xmlhttprequest == undefined || xmlhttprequest == null){
alert("XMLHttpRequest Object create failure!!");
}else{
this.xhr = xmlhttprequest;
}
//Method of sending by user
MyXMLHttpRequest.prototype.send=function(method,url,data){
if(this.xhr!=undefined && this.xhr!=null){
this.xhr.open(method,url,false);
if (method=="POST" || method=="post"){
this.xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
}
this.xhr.send(data);
return this.xhr.responseText;
}
}
}
function OnButton1() {
var xhr = new XMLHttpRequest();
var mode = 1;
if (mode==0) {
//async
xhr.open('get', '/ajax', true);
xhr.send();
xhr.onreadystatechange = function () {
if (xhr.readyState == 4) {
if (xhr.status == 200) {
var oTxt = document.getElementById('txt1');
oTxt.value = xhr.responseText;
}
}
}
}
if (mode==1) {
//sync
xhr.open('get', '/ajax?param1=1.00765¶m2=2', false);
xhr.send();
var oTxt = document.getElementById('txt1');
oTxt.value = xhr.responseText;
//=>to JSON
var obj = JSON.parse(oTxt.value);
for(var p in obj) {
alert(p+" "+obj[p]);
}
//=>toString
var str = JSON.stringify(obj);
alert(str);
}
xhr = new MyXMLHttpRequest();
var res = xhr.send('post', '/infosec', "param1=1.00765¶m2=2");
alert(res);
}
</script>
</body>
</html>
//服务端调试信息
C:\go-pro\go-https>https-ajax.exe
Server is running at localhost:8086
get 2
get 1.00765
get 2
body [123 34 105 116 101 109 34 58 34 49 34 44 34 97 109 111 117 110 116 34 58 50 46 48 55 56 57 125]
string body {"item":"1","amount":2.0789}
*simplejson.Json:&{map[item:1 amount:2.0789]}
map map[item:1 amount:2.0789]
js count 2
item 1
amount 2.0789
Finally:
我也无语,你们看着办吧!
Go web ajax project的更多相关文章
- how to create an asp.net web api project in visual studio 2017
https://docs.microsoft.com/en-us/aspnet/web-api/overview/getting-started-with-aspnet-web-api/tutoria ...
- web前端project师知识汇总
分类: Web开发应用 一.何为Web前端project师? 前端project师,也叫Web前端开发project师.他是随着web发展.细分出来的行业.Web前端开发proj ...
- Adding Swagger to Web API project
Adding Swagger to Web API project. All source code for this series can be found here. When you creat ...
- 转:Transform Web.Config when Deploying a Web Application Project
Introduction One of the really cool features that are integrated with Visual Studio 2010 is Web.Conf ...
- Web Application Project is configured to use IIS. Unable to access the IIS metabase.(配置为使用IIS Web应用程序xxxx项目。无法访问IIS元数据库。)
这几天重装系统,装了win10,居然用vs2013打开项目出现下面这个提示错误,搞了很久才知道原因: Even though I am an administrator on the machine, ...
- Eclipse中添加web dynamic project
因为我的eclipse版本是kepler service release 2,所以我用了这个链接,http://download.eclipse.org/releases/helios/ 参考链接: ...
- eclipse如何添加web dynamic project
很多eclipse版本是不能直接新建web dynamic project的,需要从网上找插件或更新. 比较方便的是在Help → Install-New-Software,点击add按钮,在Loca ...
- Web.config Transformation Syntax for Web Application Project Deployment
Web.config Transformation Syntax for Web Application Project Deployment Other Versions Updated: Ma ...
- Eclipse创建第一个Servlet(Dynamic Web Project方式)、第一个Web Fragment Project(web容器向jar中寻找class文件)
创建第一个Servlet(Dynamic Web Project方式) 注意:无论是以注解的方式还是xml的方式配置一个servlet,servlet的url-pattern一定要以一个"/ ...
随机推荐
- {03--CSS布局设置} 盒模型 二 padding bode margin 标准文档流 块级元素和行内元素 浮动 margin的用法 文本属性和字体属性 超链接导航栏 background 定位 z-index
03--CSS布局设置 本节目录 一 盒模型 二 padding(内边距) 三 boder(边框) 四 简单认识一下margin(外边距) 五 标准文档流 六 块级元素和行内元素 七 浮动 八 mar ...
- 在Ubuntu环境下安装eclipse
Eclipse运行需要Java环境,java环境的安装见https://www.cnblogs.com/Sabre/p/10349320.html,本文不再赘述. 1.下载eclipse eclips ...
- PostgreSQL+PostGIS安装以及使用
安装,参照: https://www.cnblogs.com/ytwy/p/6817179.html 创建企业级地理文件数据库时报错," You must copy the lates ...
- field, or, more generally, in a ring or even a semiring 数域、环、半环
小结: 1.数域.环.半环 :一般化.泛化 https://en.wikipedia.org/wiki/Matrix_multiplication In mathematics, matrix mul ...
- 函数调用的方法一共有 4 种,call,apply,bind
1. 每个函数都包含两个非继承而来的方法:call()方法和apply()方法. 2. 相同点:这两个方法的作用是一样的. 都是在特定的作用域中调用函数,等于设置函数体内this对象的值,以扩充函数赖 ...
- Java中抽象类和抽象方法的区别
抽象方法:在类中没有方法体的方法,就是抽象方法. 抽象类:含有抽象方法的类就叫抽象类. 抽象类中的抽象方法必须被实现! 如果一个子类没有实现父类中的抽象方法,则子类也成为了一个抽象类! 抽象类中的普通 ...
- [archlinux][daily] 自建DNS服务器 / 建立本地DNS cache / 使用dnsmasq加速上网
新公司,上网超慢,DNS竟然是远程地址,终于找到机会学习一下dnsmasq了. update@20170516: 上网慢是因为分给我的IP有限流策略,其实远端DNS并不会造成感受上的上网慢. 参考:h ...
- http请求的基本介绍
响应码 1xx:接收到请求并且继续处理 ,这个是一瞬间的状态,一般不关注 2xx:请求已被接收,理解,处理,表示正确 3xx:请求重定向 4xx:客户请求语法错误或者请求资源不存在,这个是客户端错误 ...
- Python摸爬滚打之day04----基本数据类型(列表,元组)
1.列表 列表是可变的, 有序的数据类型,列表是按照添加顺序来保存的,可以存放各种数据类型. 1.1 列表的切片(同字符串) 1.2 列表的增删改查 注意: 列表是可以直接在列表上面进行操 ...
- python解析VOC的xml文件并转成自己需要的txt格式
在进行神经网络训练的时候,自己标注的数据集往往会有数据量不够大以及代表性不强等问题,因此我们会采用开源数据集作为训练,开源数据集往往具有特定的格式,如果我们想将开源数据集为我们所用的话,就需要对其格式 ...