I took a closer look at several examples and found the problem. As we all know, WebService complies with the SOAP protocol. Why are parameters transmitted in JSON format in the example? Net WebService is compatible with JSON format, while Java is a standard WebService and is not compatible with JSON format. It seems that net has hurt everyone. So I carefully understood the WSDL file and made an example. Only the key code is shown below.
Copy codeThe Code is as follows:
$ (Function (){
$ ("# BtnWs"). click (btnAjaxPost );
});
Function btnAjaxPost (event ){
$. Ajax ({
Type: "POST ",
ContentType: "text/xml ",
Url: "http: // *****/WebServiceTest/services/HelloWorldService ",
Data: getPostData (), // JSON format is not used here
DataType: 'xml', // set to xml or not set here. Setting it to JSON format changes the return value to NULL.
Success: function (xml ){
// Parse the result in XML format.
// Browser judgment (IE and non-IE are completely different)
If ($. browser. msie ){
$ ("# Result"). append (xml. getElementsByTagName ("ns1: out") [0]. childNodes [0]. nodeValue + "<br/> ");
}
Else {
$ (Xml). find ("out"). each (function (){
$ ("# Result"). append ($ (this). text () + "<br/> ");
})
}
},
Error: function (x, e ){
Alert ('error: '+ x. responseText );
},
Complete: function (x ){
// Alert ('complete: '+ x. responseText );
}
});
}
// Define parameters that meet the SOAP protocol.
Function getPostData ()
{
// According to the WSDL analysis, sayHelloWorld is the method name, and parameters is the input parameter name.
Var postdata = "<? Xml version = \ "1.0 \" encoding = \ "UTF-8 \"?> ";
Postdata + = "<soap: Envelope xmlns: xsi = \" external "xmlns: xsd = \" http://www.w3.org/2001/XMLSchema\ "xmlns: soap = \" external "> ";
Postdata + = "<soap: Body> <sayHelloWorld xmlns = \" http://tempuri.org/\ "> ";
Postdata + = "<parameters>" + $ ("# txtName"). val () + "</parameters> ";
Postdata + = "</sayHelloWorld> </soap: Body> ";
Postdata + = "</soap: Envelope> ";
Return postdata;
}
Complete example SVN address: http://theyounglearningmaterials.googlecode.com/svn/trunk/JavaWebServices/WebServiceTest/
All of my later learning examples will be placed in http://theyounglearningmaterials.googlecode.com/svn/trunk/to facilitate management and prevent loss.