分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 网页技术

go https ajax

发布时间:2023-09-06 01:13责任编辑:顾先生关键词:http

这个很好用啊,估计大有用武之地

你会喜欢

//https-ajax.go

package main

import (
???  "fmt"
  "io"
???  "net/http"
)

func OnAjax(res http.ResponseWriter, req *http.Request) {
???  io.WriteString(res, "These are data from server")
}

func main() {
  //static web
  http.Handle("/", http.FileServer(http.Dir("web")))
  //dynamic web
  http.HandleFunc("/ajax", OnAjax)

???  // 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;
  }

  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‘, false);
      xhr.send();
      var oTxt = document.getElementById(‘txt1‘);
      oTxt.value = xhr.responseText;
    }
  }
</script>

</body>
</html>

//浏览器访问

Finally:

这个例子是win10上做的

所以,结合之前的博文,现在,你应该可以把本地windows的所有功能转移到web服务上了。

这个意义,有多大?

你看不出来!,那!就当我没说好了

go https ajax

原文地址:http://www.cnblogs.com/woodzcl/p/7574375.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved