Go HTTPS Ajax

Source: Internet
Author: User

It's very useful.

You'll like

Https-ajax.go

Package Main

Import (
"FMT"
"IO"
"Net/http"
)

Func Onajax (res http. Responsewriter, req *http. Request) {
Io. WriteString (res, "These is 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)
}
}

Small Sparrow, five Zang full

HTML:/web/ajax.html

<! DOCTYPE html>


<meta charset= "Utf-8"/>
<title>go language and Ajax examples </title>

<body>
<p><input id= "btn1" type= "button" value= "buttons"/></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>

Browser access

Finally:

This example is done on WIN10.

So, with the previous blog post, you should now be able to move all the features of local Windows to the Web service.

How big is that?

You don't see it! That! I'm not going to say that.

Go HTTPS Ajax

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.