JavaScript for Ajax

Source: Internet
Author: User

Key steps:

1 creating XMLHttpRequest objects;

2 adding a callback function;

3 Creating links;

4 Set the request header information;

5 sending data;

var xmlhttprequest;function Chaj () {if (f1.name.value==null| | f1.name.value== "") {alert ("cannot be empty"); return false;} if (window. XMLHttpRequest) {xmlhttprequest=new XMLHttpRequest ();} Else{xmlhttprequest=new ActiveXObject ("Microsoft.XMLHTTP");} Xmlhttprequest.onreadystatechange=callback;var url= "My"; Xmlhttprequest.open ("Post", url,true); Xmlhttprequest.setrequestheader ("Content-type", "application/x-www-form-urlencoded"); XmlHttpRequest.send ("Name= "+f1.name.value+" &timestamp= "+ (new Date ()). GetTime ()); function CallBack () {if (xmlhttprequest.readystate==4 && xmlhttprequest.status==200) {var result= Xmlhttprequest.responsetext;result=result.replace (/(^\s*) | ( \s*$)/g, ""); alert (result);//alert (result);}}

Note: Xmlhttpservlet is a global variable, do not forget to set Requestheadder information before sending data.



Server-side:

return information via PrintWriter;

protected void DoPost (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException { Request.setcharacterencoding ("Utf-8"); Response.setcharacterencoding ("Utf-8"); String name1=request.getparameter ("name"); String timestamp1=request.getparameter ("TimeStamp"); System.out.println (NAME1+TIMESTAMP1); PrintWriter out=response.getwriter (); Out.print ("Success"); Out.flush (); Out.close ();}


JavaScript for Ajax

Related Article

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.