• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Gowebajaxproject

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

这个是本人学习之用,比较乱

我也不知道这是在教大家什么,但如果你能看懂,那你就能真正干活了

//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 /></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&param2=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&param2=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:

我也无语,你们看着办吧!


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
简单的 Go 入门教程发布时间:2022-07-10
下一篇:
为代码编写稳定的单元测试 [Go]发布时间:2022-07-10
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap