现在的位置: 首页 > 综合 > 正文

jsd调用webservice(转)

2013年07月20日 ⁄ 综合 ⁄ 共 3444字 ⁄ 字号 评论关闭
 有时候如果只需要一个简单的无刷新的小功能,我们没必要使用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

  {
  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;
  }

抱歉!评论已关闭.