Jquery+ajax+xml

Source: Internet
Author: User

2. Previous paragraph page code

<body id= "Thebody" >
  <select id= "name" >
  	<option value= "Zhangsan" >zhangsan</option>
  	<option value= "Lisi" >lisi</option>
  </select>
   <input type= "button" id= "Button1" Value= "Get content from Server" >
  </body>


3.javabean--person

public class Person {
	private int id;
	private String name;
	private int age;
	Private String address;
	public int getId () {return
		ID;
	}
	public void setId (int id) {
		this.id = ID;
	}
	Public String GetName () {return
		name;
	}
	public void SetName (String name) {
		this.name = name;
	}
	public int getage () {return age
		;
	}
	public void Setage (int age) {
		this.age = age;
	}
	Public String getaddress () {return address
		;
	}
	public void setaddress (String address) {
		this.address = address;
	}
	
}

4. Server-side code--xmlservlet

protected void DoPost (HttpServletRequest req, HttpServletResponse resp) throws Servletexception, IOException {String
		Name = Req.getparameter ("name");
		person who = new person ();
			if ("Zhangsan". Equals (name)) {Person.setid (1);
			Person.setname ("Zhangsan");
			Person.setaddress ("Tianjin");
		Person.setage (23);
			}else{Person.setid (2);
			Person.setname ("Lisi");
			Person.setaddress ("Beijing");
		Person.setage (30);
		}//Generate XML--DOM4J Document document = Documenthelper.createdocument ();
		Element rootelement = document.addelement ("users");
		Rootelement.addcomment ("This is a Commont");
		Element userelement = rootelement.addelement ("user");
		Element idelement = userelement.addelement ("id");
		Element nameelement = userelement.addelement ("name");
		Element ageelement = userelement.addelement ("Age");
		Element addresselement = userelement.addelement ("Address");
		Idelement.settext (Person.getid () + "");
		Nameelement.settext (Person.getname ()); Ageelement.settext (PERson.getage () + "");
		
		Addresselement.settext (Person.getaddress ()); Resp.setcontenttype ("text/html; Charset=utf-8 "); Set Response head resp.setheader ("Pragma", "no-cache"); No cache Resp.setheader ("Cache-control", "No-cache");
		No cache PrintWriter out = Resp.getwriter (); OutputFormat format = Outputformat.createprettyprint ();
		
		In order to facilitate in firebug inside check format.setencoding ("Utf-8"); XMLWriter XMLWriter = new XMLWriter (Out,format);
		Write to client Xmlwriter.write (document);
	Out.flush (); }

5. Front page processing script

$ ("#button1"). Click (function () {$.ajax ({//no need to judge the browser type type: "POST",//Type URL: "Xmlser Vlet ",//Request access Servlet dataType:" xml ", data: {' name ': $ (" #name "). Val ()},//pass parameters, get the selected value Success:functio
						N (returneddata) {////The callback function successfully returned//parsing Xml--jquery object var id = $ (returneddata). FIND ("id"). Text ()////ID element found and value
						var name = $ (returneddata). Find ("name"). Text ();
						var age = $ (returneddata). Find (' age '). text ();
						
						var address = $ (returneddata). Find ("Address"). Text (); Create a table display format var html= "<table width= ' 60% ' border= ' 1 ' align= ' center ' ><tr><th>id</th><th> NAME&GT;&LT;/TH&GT;&LT;TH&GT;AGE&LT;/TH&GT;&LT;TH&GT;ADDRESS&LT;/TH&GT;&LT;/TR&GT;&LT;TR align= ' center ' >< Td> "+id+" </td><td> "+name+" </td><td> "+age+" </td><td> "+address+" </td
						></tr></table> "; $ ("#theBody table:eq (0)"). Remove ();
				Deletes the previous table $ ("#theBody"). Append (HTML);	}
				}); });

6. Front page processing script--Simple form

$ ("#button1"). Click (function () {
			$.post ("Xmlservlet",
				{
					name:$ ("#name"). Val ()
				},
				function ( Returneddata,status) {
						//Parse Xml--jquery object
						var id = $ (returneddata). FIND ("id"). Text ()///ID element found and its value
						var name = $ (returneddata). Find ("name"). text ();
						var age = $ (returneddata). Find (' age '). text ();
						var address = $ (returneddata). Find ("Address"). Text ();
						
						Create a table display format
var html= "<table width= ' 60% ' border= ' 1 ' align= ' center ' ><tr><th>id</th>< TH>NAME></TH><TH>AGE</TH><TH>ADDRESS</TH></TR><TR align= ' center ' ><td> "+id+" </td><td> "+name+" </td><td> "+age+" </td><td> "+address+" </td></tr></table> ";
						$ ("#theBody table:eq (0)"). Remove (); Delete previous table
						$ ("#theBody"). Append (HTML);
				};
			});



$ ("#button1"). Click (function () {
			$.post ("Xmlservlet",
				{
					name:$ ("#name"). Val ()
				},
				function ( Returneddata,status) {
						//Parse Xml--jquery object
						var id = $ (returneddata). FIND ("id"). Text ()///ID element found and its value
						var name = $ (returneddata). Find ("name"). text ();
						var age = $ (returneddata). Find (' age '). text ();
						var address = $ (returneddata). Find ("Address"). Text ();
						
						Create a table display format
var html= "<table width= ' 60% ' border= ' 1 ' align= ' center ' ><tr><th>id</th>< TH>NAME></TH><TH>AGE</TH><TH>ADDRESS</TH></TR><TR align= ' center ' ><td> "+id+" </td><td> "+name+" </td><td> "+age+" </td><td> "+address+" </td></tr></table> ";
						$ ("#theBody table:eq (0)"). Remove (); Delete previous table
						$ ("#theBody"). Append (HTML);
				};
			});



$ ("#button1"). Click (function () {$.ajax ({//no need to judge the browser type type: "POST",//Type URL: "Xmlser Vlet ",//Request access Servlet dataType:" xml ", data: {' name ': $ (" #name "). Val ()},//pass parameters, get the selected value Success:functio
						N (returneddata) {////The callback function successfully returned//parsing Xml--jquery object var id = $ (returneddata). FIND ("id"). Text ()////ID element found and value
						var name = $ (returneddata). Find ("name"). Text ();
						var age = $ (returneddata). Find (' age '). text ();
						
						var address = $ (returneddata). Find ("Address"). Text (); Create a table display format var html= "<table width= ' 60% ' border= ' 1 ' align= ' center ' ><tr><th>id</th><th> NAME&GT;&LT;/TH&GT;&LT;TH&GT;AGE&LT;/TH&GT;&LT;TH&GT;ADDRESS&LT;/TH&GT;&LT;/TR&GT;&LT;TR align= ' center ' >< Td> "+id+" </td><td> "+name+" </td><td> "+age+" </td><td> "+address+" </td
						></tr></table> "; $ ("#theBody table:eq (0)"). Remove ();
				Deletes the previous table $ ("#theBody"). Append (HTML);	}
				}); });


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.