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

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

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

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

Go web ajax project的更多相关文章

  1. 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 ...

  2. web前端project师知识汇总

    分类: Web开发应用  一.何为Web前端project师?           前端project师,也叫Web前端开发project师.他是随着web发展.细分出来的行业.Web前端开发proj ...

  3. 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 ...

  4. 转: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 ...

  5. 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, ...

  6. Eclipse中添加web dynamic project

    因为我的eclipse版本是kepler service release 2,所以我用了这个链接,http://download.eclipse.org/releases/helios/ 参考链接:  ...

  7. eclipse如何添加web dynamic project

    很多eclipse版本是不能直接新建web dynamic project的,需要从网上找插件或更新. 比较方便的是在Help → Install-New-Software,点击add按钮,在Loca ...

  8. Web.config Transformation Syntax for Web Application Project Deployment

    Web.config Transformation Syntax for Web Application Project Deployment Other Versions   Updated: Ma ...

  9. Eclipse创建第一个Servlet(Dynamic Web Project方式)、第一个Web Fragment Project(web容器向jar中寻找class文件)

    创建第一个Servlet(Dynamic Web Project方式) 注意:无论是以注解的方式还是xml的方式配置一个servlet,servlet的url-pattern一定要以一个"/ ...

随机推荐

  1. 洛谷1443 马的遍历【bfs】

    题目链接:https://www.luogu.org/problemnew/show/P1443 题意: 给一个n*m的棋盘,马在上面走(规则就是象棋中的规则,详细见代码dx,dy数组定义) 问棋盘上 ...

  2. 对类型“Func<,>”的引用声称该类型是在“mscorlib”中定义的,但未能找到

    报错 右击→属性

  3. [No000017A]改善C#程序的建议3:在C#中选择正确的集合进行编码

    要选择正确的集合,我们首先要了解一些数据结构的知识.所谓数据结构,就是相互之间存在一种或多种特定关系的数据元素的集合.结合下图,我们看一下对集合的分类. 集合分类 在上图中,可以看到,集合总体上分为线 ...

  4. [No0000EC]C# 字符串(String)

    在 C# 中,可以使用字符数组来表示字符串,但是,更常见的做法是使用 string 关键字来声明一个字符串变量.string 关键字是 System.String 类的别名. 创建 String 对象 ...

  5. [No0000D3]bat当前路径currentPath.bat

    @echo off echo 当前盘符:%~d0 echo 当前盘符和路径:%~dp0 echo 当前批处理全路径:%~f0 echo 当前盘符和路径的短文件名格式:%~sdp0 echo 当前CMD ...

  6. vim操作表

  7. In abstract algebra, a congruence relation (or simply congruence) is an equivalence relation on an algebraic structure (such as a group, ring, or vector space) that is compatible with the structure in

    https://en.wikipedia.org/wiki/Congruence_relation In abstract algebra, a congruence relation (or sim ...

  8. [archlinux] 迁移T7从T460s到T470

    这已经不是第一次做OS的迁移了,T7早已经迁移过多台设备了.所以,其实只需要如下三步: 1.  rsync 我一直有全系统备份的习惯,T7一直会不定期的全系统rsync到Tstation上面去.所以我 ...

  9. tornado框架&三层架构&MVC&MTV&模板语言&cookie&session

    web框架的本质其实就是socket服务端再加上业务逻辑处理, 比如像是Tornado这样的框架. 有一些框架则只包含业务逻辑处理, 例如Django, bottle, flask这些框架, 它们的使 ...

  10. 如何在windows下安装Python(Python入门教程)

    第一步:下载Python安装包 在Python的官网 www.python.org 中找到最新版本的Python安装包,点击进行下载,请注意,当你的电脑是32位的机器,请选择32位的安装包,如果是64 ...