JS調用Webservice並讀取返回XML內容

有時候如果只需要一個簡單的無刷新的小功能,我們沒必要使用AJAX組件,直接使用JS腳本調用Webservice即可實現;Webservice返回的數據是標準XML格式,所以我們需要對返回數據做處理.下面是我做的一個事例

JS代碼:

var productid = new Array();      //產品ID
var PLprice = new Array();         //最低價
var PHprice = new Array();        //最高價
var pname = new Array();          //產品名稱

function GetProductPrice(tableID,MarketId)
{
    var productCount = (document.getElementById(tableID).getElementsByTagName("input").length-1)/3;
    for(var i=0;i<productCount;i++)
    {
        productid[i] = document.getElementsByName('PID'+MarketId)[i].value;
        PLprice[i] = document.getElementsByName('PLprice'+MarketId)[i].value;
        PHprice[i] = document.getElementsByName('PHprice'+MarketId)[i].value;
        pname[i] = document.getElementById(productid[i]).innerText;
    }
    //var marketid = document.getElementsByTagName("fieldset")[0].id;
    var URL = "WebService/ProductOrderWebService.asmx/CreatePrice?productid="+productid+"&PLprice="+PLprice+"&PHprice="+PHprice+"&marketid="+MarketId;
    var browser = navigator.appName;
    var xmlhttp;
    var xmlDoc;
    if(browser == "Microsoft Internet Explorer"){           //IE瀏覽器
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        xmlDoc = new ActiveXObject('Microsoft.XMLDOM');     //創建空的XML文檔對象
    }
    else{                                                   //其它瀏覽器
        xmlhttp = new XMLHttpRequest();
        xmlDoc = document.implementation.createDocument("","",null); //創建空的XML文檔對象
    }
    xmlhttp.open("GET",URL, false);
    xmlhttp.send(null);
    var res = xmlhttp.ResponseText;
    xmlDoc.async = false;                                                              //關閉異步加載,確保在文檔完整加載之前,不會繼續執行腳本
    xmlDoc.load(xmlhttp.responseBody);                                  //加載XML文檔
    var aRowIds = xmlDoc.selectSingleNode("//string").text; //讀取string節點內容
    if(aRowIds.match('^[index.aspx]/S*')==null)                        //用正則匹配返回的是否有效URL,是則跳轉
        alert(aRowIds);
    else
        top.location.href = aRowIds;
}

WebService方法代碼:

public string CreatePrice(string productid, string PLprice, string PHprice,string marketid)
        {
            string revalue = "0";
            string[] _pidList = productid.Split(',');
            string[] _plprice = PLprice.Split(',');
            string[] _phprice = PHprice.Split(',');
            DataSet ds = SmsProductEntityAction.Get_SmsProductAll(marketid);
            DataTable tb;
            string marketname = MarketEntityAction.Get_MarketModel(marketid).MarketName;
            string msg = "";
            if (ds.Tables.Count >= 1)
            {
                for (int i = 0; i < _pidList.Length; i++)
                {
                    if (_plprice[i].Trim() != "")
                    {
                        DataRow[] row = ds.Tables[0].Select("ProductId='" + _pidList[i] + "'");
                        if (row.Length >= 1)
                        {
                            string _AttId = row[0]["AttributeID"].ToString();
                            msg += PriceGroup.AlertPriceEntity(int.Parse(_AttId), _plprice[i].ToString(), _phprice[i].ToString(), row[0]["ProductName"].ToString());
                        }
                    }
                }
                if (msg == "")
                {
                    tb = PriceGroup.CreateDataTable();
                    ArrayList al = new ArrayList(200);
                    for (int i = 0; i < _pidList.Length; i++)
                    {
                        string lp = _plprice[i].ToString();
                        string hp = _phprice[i].ToString();
                        string pid = _pidList[i].ToString();
                        if (lp.Trim() != "")
                        {
                            DataRow[] row = ds.Tables[0].Select("ProductId='" + pid + "'");
                            if (row.Length >= 1)
                            {
                                string AttId = row[0]["AttributeID"].ToString();
                                PriceEntity _Pe = PriceGroup.SetPriceEntity(int.Parse(AttId), lp, hp, pid, marketid);
                                PriceGroup.ConvertToDataTable(tb, _Pe);
                            }
                            al.Add(pid);
                        }
                    }
                    int _flag = PriceEntityAction.create_Price(tb);
                    if (_flag != 0)
                    {
                        revalue = "數據沒有正確存儲,請重新提交!";
                        //MessageBox.MsgShow(this, "數據沒有正確存儲請重新提交!");
                    }
                    else
                    {
                        Guid gi = Guid.NewGuid();
                        Session[gi.ToString()] = al;
                        revalue = "index.aspx?Subid=31&marketname=" + marketname + "&marketid=" + marketid + "&guid=" + gi.ToString();
                        //HttpContext.Current.Response.Redirect("index.aspx?Subid=31&marketname=" + marketname + "&marketid=" + marketid + "&guid=" + gi.ToString() + "");
                    }
                }
                else
                {
                    revalue = msg + "/n請查檢您的輸入是否正確!/n";
                    //msg += "//r//n請查檢您的輸入是否正確!//n";
                    //MessageBox.MsgShow(this, msg);
                }
            }
            return revalue;
        }

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