jq ajax調用java webservice和C# webservice爬坑記錄

本人net程序員一枚,最近偶然間接到一個寫接口的任務,仔細看了接口文檔才發現,該任務分三個接口,兩個提供的接口都是java寫的,於是來了興趣好好研究一波java的webservice。
進入正題:

JQ調用java webservice

由於這方面資料百度是在太少,於是只能慢慢研究,最終都是通過零星的方法綜合起來,達到我要的目的。說明下我用的ide,我用的是IntelliJ IDEA,

1.首先創建java webservice 具體網上很多

這個就可以 :idea創建webservice

2.寫webservice類

以下代碼可能會有個別錯誤,主要是應爲我只想留下結構和所需要的方法,我具體的類裏面內容寫的很多,應該是可以執行成功的,註釋也是貼上來在補充的

package example;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebParam.Mode;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import java.text.SimpleDateFormat;
import java.util.Date;

@WebService()
@SOAPBinding(style = SOAPBinding.Style.RPC)
public class CKInfoService
{//關於@等註解,我回在下面貼鏈接,也是我辛苦尋找來的東西呀
	@WebMethod()// "獲取查覈任務"
	    public String getTask(@WebParam(mode = Mode.IN) String in0, @WebParam(mode = Mode.IN) String in1, @WebParam(mode = Mode.IN) String in2)
	    {//參數沒有@WebParam(mode = Mode.IN)參數可能jq調用後參數值都爲null,in  和out 我沒具體研究 估計90%應該都是用in吧,大家可以看下面鏈接仔細研究
	        String result = "";
	        String code ="0000";
	        System.out.println("2接口參數0:" + in0 + "   1:" + in1 + "  2:" + in2);
	        String msg = "{\"message\":{\"task\":\"" + result + "\",\"info\":\"提示信息666\"},\"statusCode\":\"" + code + "\"}";	
	        return msg;
    }
}

貼上註解說明鏈接:@webservice說明,當然還有更詳細的,我覺得這些應該是夠用了。
還要說明的是,這個需要發佈成webservice的類寫好後按照上面連接的方法生成wsdl文件後,大致結構都是差不多的,
貼個wsdl結構連接吧:wsdl結構介紹

3.創建測試用html並引用jq

我的測試html 內容是這樣的

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>首頁</title>
</head>
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<body>
首頁
<button onclick="get()">點擊</button>
<p id="pname"></p>
<p id="pmsg"></p>
</body>
<script type="text/javascript">
    function get() {
        var url = "http://localhost:8888/CKInfoService";
        var method = 'getTask';
        var pam0 = "arg0";
        var pam1 = "arg1";
        var pam2 = "arg2"; 
        //jq想要調用java webservice 我目前就找到了這個方式 需要拼接成soap通信的結構,這個格式是固定的,下面我貼出我參考的文章連接
        var p1 = '<?xml version="1.0" encoding="utf-8"?>' +
            '<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://example/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">' +
                //'<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">'
            '<soap:Body>' +
            '<p:' + method + '>' + 
            //'<' + method + ' xmlns="http://example/">' + 
            '<' + pam0 + '>1</' + pam0 + '>' +
            '<' + pam1 + '>1</' + pam1 + '>' +
            '<' + pam2 + '>1</' + pam2 + '>' +
            '</p:' + method + '>' +
            //'</' + method + '>' +
            '</soap:Body>' +
            '</soap:Envelope>';
        var p2 = '{"' + pam0 + '":"1","' + pam1 + '":"1","' + pam2 + '":"1"}';
        var obj = ajax(p1, url);
        if (!!obj) {
            alert(obj);
            $('#pmsg').text(obj);
        }
    }

    function ajax(param, url) {
        var res = null;
        if (!param) {
            return res;
        }
        $.ajax({
            url: url,
            data: param,
            contentType: "text/xml",//jq格式一定是xml的
            type: "POST",
            async: false,
            dataType: 'xml',//返回值的類型可以不設,建議設置成xml,應爲返回值是xml格式的,方便成功返回後取值,這個類型設置不對,會導致請求錯誤進入到error方法裏
            success: function (data) {
                if (!!data) {
                    res = data.getElementsByTagName("return")[0].innerHTML;
                }
            },
            error: function (err) {
                alert(err.responseText);
            }
        });
        return res;
    }
</script>
</html>

貼上請求數據拼接的參考的連接:拼接JQ請求的data
這裏很容易遇到的錯誤就是後臺獲取不到參數,所有參數值都是null,按照我寫的這個data格式應該是沒有問題的,我是解決過的
網上還有另一種格式 兩種很區別不大 唯一區別就是命名空間的聲明那裏。就是我構建請求data註釋的那幾行,對比就會發現差別,用註釋的那種寫法就會拿不到參數值
好了 需要的我都貼出來了在貼一張結果圖吧
在這裏插入圖片描述
這條消息就是後臺輸出的結果。

jq調用C#webservice

創建就沒什麼說的,網上關於C# webservice的內容一大堆,其實調用也不用我說也有很多 我還是小說一下吧
ajax這樣寫就可以,一般都是json格式上傳和返回數據,不需要java那樣麻煩

$.ajax({
            contentType: "application/json",
            url: "http//:localhost/Service/ImportBuildingService.asmx/GetViewDYH",
            data: "{inparm:'" + 6666+ "'}",       //GetViewDYH,是需要訪問的webservice方法
            type: "POST",
            async: false,
            dataType: 'xml',
            success: function (data) {
                if (!!data) {
                   alert(data)
                }
            },
            error: function (err) {
                alert(err.responseText);
            }
        });
[WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // 若要允許使用 ASP.NET AJAX 從腳本中調用此 Web 服務,請取消註釋以下行。 創建WebService文件時下面一行是註釋的,要jq能夠訪問到必須取消下面一行的註釋
    [System.Web.Script.Services.ScriptService]
    public class ImportBuildingService: System.Web.Services.WebService
    {
[WebMethod(EnableSession = false, Description = "服務")]
        public string GetViewDYH(string inparm)
        {
        string a="{\"a\":\"6\"}";
        return a;//返回的是xml格式//<string xmlns="http://tempuri.org/">{\"a\":\"6\"}</string>
        //Context.Response.Write(a);//頁面格式直接輸出 {\"a\":\"6\"} 一般jq ajax數據交互使用這種方式
        }
    }

相對來說C#的webservice要方便操作很多 也許是我比較熟悉,如果各位有jq訪問java webservice更方便的方法 請教給我 一定虛心學習!

如有錯,請各位指正 ! 隨便點個讚唄~~~

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章