I recently learned jquery and found many examples of jquery Accessing. Net WebService. WebService interfaces should be generic. Why is there no Java example? This has aroused my interest.
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.
1 $ (function (){
2 $ ("# btnws"). Click (btnajaxpost );
3 });
4
5 function btnajaxpost (event ){
6 $. Ajax ({
7 Type: "Post ",
8 contenttype: "text/XML ",
9 URL: "http: // *****/webservicetest/services/helloworldservice ",
10 Data: getpostdata (), // The JSON format is not used here
11 datatype: 'xml', // set it to XML or not set here. Setting it to JSON format changes the return value to null.
12 success: function (XML ){
13 // parse the result in XML format.
14 // browser judgment (ie and non-ie are completely different)
15 if ($. browser. MSIE ){
16 $ ("# result"). append (XML. getelementsbytagname ("NS1: Out") [0]. childnodes [0]. nodevalue + "<br/> ");
17}
18 else {
19 $ (XML). Find ("out"). Each (function (){
20 $ ("# result"). append ($ (this). Text () + "<br/> ");
21 })
22}
23 },
24 error: function (x, e ){
25 alert ('error: '+ X. responsetext );
26 },
27 complete: function (x ){
28 // alert ('complete: '+ X. responsetext );
29}
30 });
31}
32 // define parameters that meet the SOAP protocol.
33 function getpostdata ()
34 {
35 // according to the WSDL analysis, sayhelloworld is the method name, and parameters is the input parameter name.
36 var postdata = "<? XML version = \ "1.0 \" encoding = \ "UTF-8 \"?> ";
37 postdata + = "<soap: envelope xmlns: xsi = \" external "xmlns: XSD = \" http://www.w3.org/2001/xmlschema\ "xmlns: Soap = \" external "> ";
38 postdata + = "<soap: Body> <sayhelloworld xmlns = \" http://tempuri.org/\ "> ";
39 postdata + = "<parameters>" + $ ("# txtname"). Val () + "</parameters> ";
40 postdata + = "</sayhelloworld> </soap: Body> ";
41 postdata + = "</soap: envelope> ";
42 return postdata;
43}
Complete example SVN address:HTTP: // Theyounglearningmaterials.googlecode.com/svn/trunk/javawebservices/webservicetest/
All my learning examples will be put inHTTP: // Theyounglearningmaterials.googlecode.com/svn/trunk/to prevent loss.