怎么样能够用prototype.js中的ajax对象访问webservices 顶者有分 怎么样能够用prototype.js中的ajax对象访问webservices.顶者有分回答正确者另外开帖加分 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 提供想法:通过web页调用websevice再返回ajax 1.在web页面.cs里调用WEBSERVICES,得到数据2.将1得到的数据传给prototype.js里获得数据的方法即可 1 prototype.js 1.5 不能使用soap的方式去访问webservice至少我这里是不行的.我还是从官方下的我这里提供两中一 : soap 这种是比较标准的,至于要解析也是很简单的,用正则解析就好了拿我以前写的页面脚本 /js/record.js // JScript 文件 function GetApplicationByKey(varKey) { var url = "/WebService.asmx" ; //路径 var uri = "http://tempuri.org" ; // uri var xmlhttp = new ActiveXObject('Microsoft.XMLHTTP'); if(xmlhttp == null)return; var soapaction = uri + "/" + 'GetApplicationByKey' ; xmlhttp.Open("POST",url,false, "","") ; xmlhttp.setRequestHeader("SOAPAction", soapaction) ; xmlhttp.setRequestHeader("Content-Type","text/xml") ; var content = ' <?xml version="1.0" encoding="utf-8"?> <soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope"> <soap12:Body> <GetApplicationByKey xmlns="http://tempuri.org/"> <key> '+varKey+' </key> </GetApplicationByKey> </soap12:Body> </soap12:Envelope> '; xmlhttp.Send (content) ; //发送soap var rt=xmlhttp.responseText; ///得到返回的xml var s = getTextByTagName(rt,'GetApplicationByKeyResult'); //解析xml得到直 alert(s); } function getTextByTagName(s,tag) //此处解析{ var pattern; eval('pattern = /\\ <'+tag+'\> .*?\\ <[/]'+tag+'\> /g;'); var matches = s.match(pattern); for(var i=0 ; i < matches.length ; i++) { var t = matches[i]; eval('pattern = /\\ <'+tag+'\> (.*?)\\ <[/]'+tag+'\> /g;'); pattern.exec(t); matches[i] = RegExp.lastParen; } return matches; } // 三,webservice代码webservice.asmx using System; using System.Web; using System.Collections; using System.Web.Services; using System.Web.Services.Protocols; /// <summary> /// WebService 的摘要说明 /// </summary> [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] public class WebService : System.Web.Services.WebService { public WebService () { //如果使用设计的组件,请取消注释以下行 //InitializeComponent(); } [WebMethod] public string HelloWorld() { return "Hello World"; } [WebMethod] public string GetApplicationByKey(string key) { object o = HttpContext.Current.Application.Get(key); if (o == null) return "无返回值"; return o.ToString(); } } 二 :prototype.js调用脚本function GetApplicationByKey(varkey){ var url = "/WebService.asmx/GetApplicationByKey" ; //路径 var content = "key="+escape(varkey);//如果有多个参数 key1=varkey1&key2=varkey2.......用&来连接 var myAjax = new Ajax.Request(url,{method:'post',onComplete: getTradeShows,postBody:content });}function getTradeShows(oriHttp){ var xmlDoc; var result ; if(window.ActiveXObject) //解析 { //IE xmlDoc = new ActiveXObject('Microsoft.XMLDOM'); xmlDoc.async = false; xmlDoc.loadXML(oriHttp.responseText); result = xmlDoc.getElementsByTagName("string")[0].text; } else if (document.implementation&&document.implementation.createDocument) { //firefox //xmlDoc = document.implementation.createDocument('', '', null); var sXml = oriHttp.responseText ; var oParser = new DOMParser(); xmlDoc = oParser.parseFromString(sXml,"text/xml"); result = xmlDoc.getElementsByTagName("string")[0].textContent ; } else { alert('xml parser can\'t create ,pleaset update internet exlporer to 5.5 '); return; } alert(result);} http://topic.csdn.net/u/20071226/08/bf47c50b-c105-4323-887f-82e66e44a2de.html请看这里 GridView 的行高 求一个验证的做法 查询数据问题~ VS2010里创建ado.net实体模型 求一汇总语句 <div id="d">nds</div>如何获得div标签的值?(nds) 急!web.config的设置问题 关于ASP:TEXTBOX的控件出现的问题 **********动态生成的控件,在动态生成的表单元格中,怎样实现强制换行?********* 未找到类型“product”的公共成员????为什么会这样! 脚本写验证 datalist模板中的控件不能在后台引用吗?
2.将1得到的数据传给prototype.js里获得数据的方法即可
至少我这里是不行的.我还是从官方下的我这里提供两中
一 : soap 这种是比较标准的,至于要解析也是很简单的,用正则解析就好了
拿我以前写的页面脚本 /js/record.js
// JScript 文件
function GetApplicationByKey(varKey)
{ var url = "/WebService.asmx" ; //路径
var uri = "http://tempuri.org" ; // uri
var xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
if(xmlhttp == null)return;
var soapaction = uri + "/" + 'GetApplicationByKey' ;
xmlhttp.Open("POST",url,false, "","") ;
xmlhttp.setRequestHeader("SOAPAction", soapaction) ;
xmlhttp.setRequestHeader("Content-Type","text/xml") ;
var content = ' <?xml version="1.0" encoding="utf-8"?> <soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope"> <soap12:Body> <GetApplicationByKey xmlns="http://tempuri.org/"> <key> '+varKey+' </key> </GetApplicationByKey> </soap12:Body> </soap12:Envelope> ';
xmlhttp.Send (content) ; //发送soap
var rt=xmlhttp.responseText; ///得到返回的xml
var s = getTextByTagName(rt,'GetApplicationByKeyResult'); //解析xml得到直
alert(s);
}
function getTextByTagName(s,tag) //此处解析
{
var pattern;
eval('pattern = /\\ <'+tag+'\> .*?\\ <[/]'+tag+'\> /g;');
var matches = s.match(pattern); for(var i=0 ; i < matches.length ; i++)
{
var t = matches[i];
eval('pattern = /\\ <'+tag+'\> (.*?)\\ <[/]'+tag+'\> /g;'); pattern.exec(t);
matches[i] = RegExp.lastParen;
}
return matches;
} //
三,webservice代码webservice.asmx using System;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
/// <summary>
/// WebService 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class WebService : System.Web.Services.WebService { public WebService () { //如果使用设计的组件,请取消注释以下行
//InitializeComponent();
} [WebMethod]
public string HelloWorld() {
return "Hello World";
}
[WebMethod]
public string GetApplicationByKey(string key)
{
object o = HttpContext.Current.Application.Get(key);
if (o == null) return "无返回值";
return o.ToString();
}
} 二 :prototype.js调用脚本function GetApplicationByKey(varkey)
{ var url = "/WebService.asmx/GetApplicationByKey" ; //路径
var content = "key="+escape(varkey);
//如果有多个参数 key1=varkey1&key2=varkey2.......用&来连接
var myAjax = new Ajax.Request(url,{method:'post',onComplete: getTradeShows,postBody:content });}
function getTradeShows(oriHttp)
{
var xmlDoc;
var result ;
if(window.ActiveXObject) //解析
{
//IE
xmlDoc = new ActiveXObject('Microsoft.XMLDOM');
xmlDoc.async = false;
xmlDoc.loadXML(oriHttp.responseText);
result = xmlDoc.getElementsByTagName("string")[0].text;
}
else if (document.implementation&&document.implementation.createDocument)
{
//firefox
//xmlDoc = document.implementation.createDocument('', '', null);
var sXml = oriHttp.responseText ;
var oParser = new DOMParser();
xmlDoc = oParser.parseFromString(sXml,"text/xml");
result = xmlDoc.getElementsByTagName("string")[0].textContent ; }
else
{
alert('xml parser can\'t create ,pleaset update internet exlporer to 5.5 ');
return;
} alert(result);
}