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一定要以一个"/ ...
随机推荐
- 洛谷P1403 约数研究【思维】
题目:https://www.luogu.org/problemnew/show/P1403 题意: 定义$f(n)$为n的因子个数.给定一个数n,求$f(1)$到$f(n)$之和. 思路: 最直接的 ...
- 深度学习模型融合stacking
当你的深度学习模型变得很多时,选一个确定的模型也是一个头痛的问题.或者你可以把他们都用起来,就进行模型融合.我主要使用stacking和blend方法.先把代码贴出来,大家可以看一下. import ...
- Mysql 常用调优命令总结
在工作中,经常会碰到Mysql性能问题,本文记录了调优时会用到的一些命令: 1. show full processlist\G: 显示Mysql服务器正在执行的线程.有root权限的用户可以显示所有 ...
- Gif动态图UIImage
#pragma mark 动态图生成 -(UIImage *)getAnimatedGIFWithData:(NSString *)path { NSData *data = [NSData data ...
- 2016年蓝桥杯省赛A组c++第3题(图论)
/* 有一个含有10个格子的图形,现用0~9填充,连续的数不能填充在相邻的格子中(包括对角线相邻). 现每个数只能填写一次,问有多少种填充方法? 0111 1111 1110 (1表示有格子,0表示没 ...
- python-多线程等概念
并发 & 并行 并发:是指系统具有处理多个任务的能力 并行:是指系统具有 同时 处理多个任务的能力 并行 是 并发的一个子集 同步 & 异步 同步:当进程执行到一个I/O(等待外部数 ...
- 《HTTP - http首部信息》
推荐一首歌 - 僕が死のうと思ったのは (曾经我也想过一了百了) 也就听了几十遍而已 经历一番波折,终于正式到了北京. 刚开始是很艰难的,多走两步就好了,不是么. 1:首部字段 Cache-Contr ...
- (4.25)格式化T-SQL工具
写代码的一个重要方面是格式化.没有人喜欢格式错误的代码.对于继承它的人,甚至是作者,他/她在一周左右后检查它是很难理解/维护的 我确实理解开发人员在严格的交付准则下工作,没有人会想要延迟交付,而不是格 ...
- chkconfig 管理系统服务
[root@localhost ~]# chkconfig --list # 列出开启或关闭了哪些系统服务 [root@localhost ~]# chkconfig xxx off # 关闭某个系统 ...
- 小程序支持连Wi-Fi,代码包到4M
小程序又开发新能力了:1 更多硬件连接功能等着你.在商场等场所,用户以往要用微信连Wi-Fi,要扫二维码并关注公众号,点击菜单里的“连Wi-Fi”才能使用上网络.连个Wi-Fi何必让用户经过两道坎? ...