First, let's look at the example.
Java code
$. Ajax ({
Url: '/platform/contact/resetStatus. do? ActionType = test ',
Type: 'post ',
DataType: 'xml', // do not write text or html here !!!
Data: "& name = John ",
Timeout: 1000,
Error: function (){
Alert ('error loading XML document ');
},
Success: function (xml ){
Alert (xml );
$ (Xml). find ("POLICY_INFO"). each (function (I ){
Var id = $ (this). children ("policy_id"); // retrieves an object
Var idvalue = $ (this). children ("policy_code"). text (); // get text
});
}
});
Java code
Java code
Public void test (HttpServletRequest request, HttpServletResponse response)
Throws Exception {
// Search for the primary table
PrintWriter out = response. getWriter ();
Response. setContentType ("text/xml ");
String name = request. getParameter ("name ");
System. out. println ("name =" + name );
String xml = "<? Xml version = '1. 0' encoding = \ "UTF-8 \"?> <Callcenter_response> ";
Xml + = "<action_id> POLICY_INFO </action_id> <record> <policy_id> 19049059 </policy_id> <policy_code> 000486474153008 </policy_code> </record> </callcenter_response>";
// Out. write ("true"); // return to xmlgiftHttp. responseText
Out. write (xml );
// Out. println (xml );
Out. flush ();
Out. close ();
System. out. println ("xml =" + xml );
System. out. println ("success ..................");
}
1. Pay attention to the $. ajax attribute DataType: 'xml'
2. in Java, the response. setContentType ("text/xml") in Out. println must also be in XML format.
Author: "ChinaJweb"