Ajax打开三种页面的请求
xmlhttprequest对象可以打开两种方式的页面请求
<webServices >
<protocols >
<add name ="HttpGet"/>
</protocols>
</webServices>
2,在WebSerVice.asmx文件中写一个方法getResult,该方法接受两个字符型的参数,其中的WebMethod属性不可以漏掉。。。
{
[WebMethod]
public string getResult(string str1,string str2)
{
return "输入的第一个字符串是:" + str1 + "\n输入的第二个字符串是:" + str2;
}
}
//以下为创建xmlHttpRequest对象
var xmlHttpRequest=false;
try
{
xmlHttpRequest=new XMLHttpRequest();//非IE浏览器
}
catch (microsoft)//IE浏览器
{
try
{
xmlHttpRequest=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (othermicrosoft)
{
try
{
xmlHttpRequest=new ActiveXObject("Microsoft.XMLHTTP");
}
catch (failed)
{
xmlHttpRequest=false;
}
}
}
if (!xmlHttpRequest)
{
alert("不能初始化XMLHttpRequest对象!");
}
//相应button的Click事件
function callback()
{
var arg1=document.getElementById ("Text1").value;//获取Text1文本框的值
var arg2=document.getElementById ("Text2").value;//获取Text2文本框的值
var url="WebService.asmx/getResult?str1="+escape(arg1)+"&str2="+escape(arg2);//要打开的url地址,并传递两个参数,这里参数名必须同webservice提供的参数名一致
xmlHttpRequest.open("get",url,true);//以get方式打开指定的url请求,并且使用的是异步调用方式(true)
xmlHttpRequest.onreadystatechange=updatePage;//指定回调函数updatePage
xmlHttpRequest.send(null);//发送请求,由于是get方式,这里用null
}
//回调函数
function updatePage()
{
if (xmlHttpRequest.readyState==4)
{
if (xmlHttpRequest.status==200)
{
var response=xmlHttpRequest.responseXML;//以xml格式回调内容
var result;
if (response.evaluate)//XML Parsing in Mozilla
{
result=response.evaluate("//text()",response,null,XpathResult.STRING_TYPE,null).stringValue;//Mozilla中获取xml中的文本内容
}
else
{
//XML Parsing in IE
result=response.selectSingleNode("//text()").data;//IE中获取xml中的文本内容
}
document.getElementById ("TextArea1").value=result;
}
}
}
</script>
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->protected void Page_Load(object sender, EventArgs e)
{
string str1 = Request["argA"].ToString();
string str2 = Request["argB"].ToString();
Response.Write("输入的第一个字符串是:" + str1 + "\n输入的第二个字符串是:" + str2);
}
//以下为创建xmlHttpRequest对象
var xmlHttpRequest=false;
try
{
xmlHttpRequest=new XMLHttpRequest();//非IE浏览器
}
catch (microsoft)//IE浏览器
{
try
{
xmlHttpRequest=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (othermicrosoft)
{
try
{
xmlHttpRequest=new ActiveXObject("Microsoft.XMLHTTP");
}
catch (failed)
{
xmlHttpRequest=false;
}
}
}
if (!xmlHttpRequest)
{
alert("不能初始化XMLHttpRequest对象!");
}
//相应button的Click事件
function callback()
{
var arg1=document.getElementById ("Text1").value;//获取Text1文本框的值
var arg2=document.getElementById ("Text2").value;//获取Text2文本框的值
var url="ReturnStr.aspx?argA="+escape(arg1)+"&argB="+escape(arg2);//要打开的url地址
xmlHttpRequest.open("get",url,true);//以get方式打开指定的url请求,并且使用的是异步调用方式(true)
xmlHttpRequest.onreadystatechange=updatePage;//指定回调函数updatePage
xmlHttpRequest.send(null);//发送请求,由于是get方式,这里用null
}
//回调函数
function updatePage()
{
if (xmlHttpRequest.readyState==4)
{
if (xmlHttpRequest.status==200)
{
var response=xmlHttpRequest.responsetext;//回调的内容,以文本的方式返回,当然也可以以xml方式返回(写法为xmlHttpRequest.responseXML)
// alert(response);//这里返回的不仅有文本,还有诸如.aspx文件中的各种标签
// var result=response.split('<')[0];//所以这里要使用split来取文本内容
var res=response.split('<');
var result=res[0];
document.getElementById ("TextArea1").value=result;
}
}
}
</script>
{
if (Request["argA"]!=null && Request["argB"]!=null)//这个判断一定要加上
{
string str1 = Request["argA"].ToString();//获取客户端发送过来的参数的值
string str2 = Request["argB"].ToString();
Response.Write("输入的第一个字符串是:" + str1 + "\n输入的第二个字符串是:" + str2);
}
}
//以下为创建xmlHttpRequest对象
var xmlHttpRequest=false;
try
{
xmlHttpRequest=new XMLHttpRequest();//非IE浏览器
}
catch (microsoft)//IE浏览器
{
try
{
xmlHttpRequest=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (othermicrosoft)
{
try
{
xmlHttpRequest=new ActiveXObject("Microsoft.XMLHTTP");
}
catch (failed)
{
xmlHttpRequest=false;
}
}
}
if (!xmlHttpRequest)
{
alert("不能初始化XMLHttpRequest对象!");
}
//相应button的Click事件
function callback()
{
var arg1=document.getElementById ("Text1").value;//获取Text1文本框的值
var arg2=document.getElementById ("Text2").value;//获取Text2文本框的值
var url="Default.aspx?argA="+escape(arg1)+"&argB="+escape(arg2);//要打开的url地址,并传递两个参数
// var url="ReturnStr.aspx?argA="+escape(arg1)+"&argB="+escape(arg2);
// var url="WebService.asmx/getResult?str1="+escape(arg1)+"&str2="+escape(arg2);//要打开的url地址,并传递两个参数,这里参数名必须同webservice提供的参数名一致
xmlHttpRequest.open("get",url,true);//以get方式打开指定的url请求,并且使用的是异步调用方式(true)
xmlHttpRequest.onreadystatechange=updatePage;//指定回调函数updatePage
xmlHttpRequest.send(null);//发送请求,由于是get方式,这里用null
}
//回调函数
function updatePage()
{
if (xmlHttpRequest.readyState==4)
{
if (xmlHttpRequest.status==200)
{
var response=xmlHttpRequest.responsetext;//回调的内容,以文本的方式返回,当然也可以以xml方式返回(写法为xmlHttpRequest.responseXML)
// alert(response);//这里返回的不仅有文本,还有诸如.aspx文件中的各种标签
// var result=response.split('<')[0];//所以这里要使用split来取文本内容
var res=response.split('<');
var result=res[0];
document.getElementById ("TextArea1").value=result;
// var response=xmlHttpRequest.responseXML;//以xml格式回调内容
// var result;
// if (response.evaluate)//XML Parsing in Mozilla
// {
// result=response.evaluate("//text()",response,null,XpathResult.STRING_TYPE,null).stringValue;//Mozilla中获取xml中的文本内容
// }
// else
// {
// //XML Parsing in IE
// result=response.selectSingleNode("//text()").data;//IE中获取xml中的文本内容
// }
// document.getElementById ("TextArea1").value=result;
}
}
}
</script>
最新文章
- PHP安装
- Ubuntu下安装php7后无法启动Apache
- 无法加载协定为“ServiceReference1.xxxxx”的终结点配置部分,因为找到了该协定的多个终结点配置。请按名称指示首选的终结点配置部分
- js中Math.random()生成指定范围数值的随机数
- 动态调用webservice 接口
- POJ1004Financial Management
- Entity Framework 技术参考:http://kb.cnblogs.com/zt/ef/
- Servlet程序开发-- 过滤器
- Hibernate映射乱码
- 判断是否AVL平衡二叉书
- 使用Mermaid画图
- AjaxPro2完整入门教程
- ABP+AdminLTE+Bootstrap Table权限管理系统第七节--登录逻辑及几种abp封装的Javascript函数库
- QMessageBox的用法
- kubernetes1.3搭建dns服务
- 如何将Excel 图表导出
- HttpRequest中常见的四种Content-Type(转)
- java工程师
- 2013-8-6 ubuntu基本操作
- 一行转多行 及多行转一行的 hive语句
热门文章
- Linux中U盘和SD卡加载卸载命令
- Go语言实战_自己定义OrderedMap
- MHA 一主两从搭建-脚本VIP-自动切换
- jboss-as-7.1.1.Final与jdk1.8不兼容解决方案
- ios开发之核心动画四:核心动画-Core Animation--CABasicAnimation基础核心动画
- 数据库筛选用户,然后去掉一部分(列表求差),再随机返回一个用户。sqlalchemy + python集合(set) + random
- html5-5 HTML5表单元素和内嵌框架
- css3-4 css3边框样式
- NetSnmp配置
- [Angular] Updating and resetting FormGroups and FormControls