Note: This article is the most basic AJAX transmission code collation, the basic part of the practice, the concept part of the self-check.
Code Usage Library: http://lib.sinaapp.com/js/jquery/2.0.2/jquery-2.0.2.min.js
1. Ajax format
var data = {"Dataid": $ ("#data"). Val ()};$.ajax ({ data:data, dataType:"JSON", // data type / content format URL: "/index.html", Type:// transmission mode Success:function(data,textstatus) { alert ("data returned successfully");} });
The data is transferred to the corresponding URL, and the above is a way to package the packets.
This data transfer Protocol is the HTTP protocol.
2, JS in the HTTP server to get data
varHTTP = require (' http ');varFS = require (' FS ');//introducing a file reading modulevarServer = Http.createserver (function(req, res) {varurl = req.url;//req = Request, res = Response if(url = = "/index.html")) {Req.on (' Data ',function(data) {vardata = decodeuricomponent (data) + "";//Get the data varStrjson =json.stringify (user); Res.writehead ($, {' Content-type ': ' Text/plain '}); Res.write (Strjson); //Send DataRes.end (); }); Req.on (' End ',function(){ //Read End }); varFile = DocumentRoot +URL; //parameter 1: The path of the file to be read; //Parameter 2: callback function, read failed information in err, err is null means there is no error. Data read to the file varFile = documentroot + URL;//Absolute AddressFs.readfile (file,function(err, data) {varType = ' text/html '; varname = [/.css/,/.html/,/.js/]; varTypeName = [' Text/css ', ' text/html ', ' text/javascript ']; for(varIinchname) { if(name[i].test (file)) {type=Typename[i]; Break; } } //match the method and file name if(Err) {Res.writeheader (404, {' Content-type ': type+ '; charset= ' Utf-8 '}); Res.write (' ); Res.end (); } Else{Res.writeheader ("Content-type ': type+ '; charset=" Utf-8 "}); Res.write (data); Res.end ();} }); //open the corresponding HTML file (including the referenced CSS and JS, if there are other types, self-added, there is a simpler way, self-lookup)}}). Listen (8889, "127.0.0.1");//The 8889 here is the port number of the listener, which can be configured according to your needs, and be careful not to conflict with the port number used by some local applications.
3. Using Tomcat server to get data in Java EE
//sample background, according to the class, to obtain students ' informationString classid = Request.getparameter ("ClassID"); Jsonarray Jsonarray=NewJsonarray (); Jsonobject Jsonobj=NewJsonobject (); for(intI=0;i<data.size (); i++) {System.out.println (Data.get (i). Getstuid ()); Data.get (i). Setstuname (Studao.readstudent (Data.get (i). Getstuid ()). Getstuname ()); Jsonobj.put ("Stuid", Data.get (i). Getstuid ()); Jsonobj.put ("SCID", Data.get (i). Getscid ()); Jsonobj.put ("Claid", Data.get (i). Getclaid ()); Jsonobj.put ("Stuname", Data.get (i). Getstuname ()); Jsonarray.add (jsonobj);} PrintWriter out=Response.getwriter (); out.println (Jsonarray);
The above two methods return data, in the AJAX success as array processing, if not rest assured, output debugging a bit ~
ex state, Easter Egg
Alert ("Delete succeeded"// page tip box location.reload (); // Refresh the current page var jq172 = jquery.noconflict (true); </script> (function($) { $ ("#user");}) (jq172); // jquery Conflict resolution method
Ajax on the Java EE and JS server data Base Exchange