xmlhttprequest对象可以打开两种方式的页面请求

1,.asmx格式的webservice页面。
2,.aspx格式的web窗体
其中web窗体可以是调用一新建的web窗体,同时调用和被调用页面可以是同一个页面,表达的可能不够清楚,还是看示例吧。
 
一、调用.asmx格式的webservice页面
 
1,新建一个ASP.NET应用程序,在该应用程序中添加一web服务页面(webservice页面),此时项目中有三个页面,web窗体页面(Default.aspx)、webservice页面(WebSerVice.asmx)和配置页面(Web.config),为了能够使用HTTP GET 方式访问web服务,需要在Web.config文件中的<system.web></system.web>标签对中加入如下配置
 
<webServices >
<protocols >
<add name ="HttpGet"/>
</protocols>
</webServices>

2,在WebSerVice.asmx文件中写一个方法getResult,该方法接受两个字符型的参数,其中的WebMethod属性不可以漏掉。。。

 
public class WebService : System.Web.Services.WebService
    {
        [WebMethod]
        public string getResult(string  str1,string  str2)
        {
            return "输入的第一个字符串是:" + str1 + "\n输入的第二个字符串是:" + str2;
        }
    }
 
3,布局Default.aspx文件如下
 
 
4,在Default.aspx文件中写入如下JavaScript脚本
 
<script language="javascript" type ="text/javascript" >
    //以下为创建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>
 
5,运行,是不是看到自己想看到的结果。这就是ajax打开webservice页面请求的方式
 
 
二、调用.aspx格式的web窗体(新建web窗体)
 
1,在上面新建的ASP.NET应用程序中添加一名为ReturnStr的web窗体,在ReturnStr.aspx.cs文件中的Page_Load事件中添加如下代码:
 


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

 
2,修改Default.aspx文件中的JavaScript脚本如下
 
<script language="javascript" type ="text/javascript" >
    //以下为创建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>
 
3,运行结果,同上。。。
 
三、调用.aspx格式的web窗体(名为Default的web窗体,无需另外创建)
 
1,点击上面新建的ASP.NET应用程序中Default窗体,在Default.aspx.cs文件中的Page_Load事件中添加如下代码:
 
protected void Page_Load(object sender, EventArgs e)
        {
            if (Request["argA"]!=null && Request["argB"]!=null)//这个判断一定要加上
            {
                string str1 = Request["argA"].ToString();//获取客户端发送过来的参数的值
                string str2 = Request["argB"].ToString();
                Response.Write("输入的第一个字符串是:" + str1 + "\n输入的第二个字符串是:" + str2);
            }
        }
 
2,修改Default.aspx文件中的JavaScript脚本如下,这里其实这是对二中的url进行了修改,改变指定的url地址。下面的代码包含了这篇文章中所涉及到的所有的代码,只是这个部分没用到的代码注释起来了,这样也是为了自己方便查看这三个不同的打开方式之间,它们的代码的异同,同时也方便他人。
 
   <script language="javascript" type ="text/javascript" >
    //以下为创建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>

 
3,运行,结果可靠。。。

Ajax打开三种页面的请求的更多相关文章

  1. Ajax的三种实现及JSON解析

    本文为学习笔记,属新手文章,欢迎指教!! 本文主要是比较三种实现Ajax的方式,为以后的学习开个头. 准备: 1.  prototype.js 2.  jquery1.3.2.min.js 3.  j ...

  2. MVC异步AJAX的三种方法(JQuery的Get方法、JQuery的Post方法和微软自带的异步方法)

    异步是我们在网站开发过程中必不可少的方法,MVC框架的异步方法也有很多,这里介绍三种方法: 一.JQuery的Get方法 view @{ Layout = null; } <!DOCTYPE h ...

  3. ajax 使用 三种方法 设置csrf_token的装饰器

    1. CSRF中间件   CSRF跨站请求伪造 2. 补充两个装饰器  from django.views.decorators.csrf import csrf_exempt, csrf_prote ...

  4. [Web 前端] 006 css 三种页面引入的方法

    1. 外链式 用法 step 1: 在 html 文档的 head 头部分写入下方这句话 <link rel="stylesheet" href="./xxx.cs ...

  5. Jquery Ajax处理,服务端三种页面aspx,ashx,asmx的比较

    常规的Jquery Ajax 验证登录,主要有3种服务端页面相应 ,也就是 aspx,ashx,asmx即webserivice . 下面分别用3种方式来(aspx,ashx,asmx)做服务端来处理 ...

  6. web三种跨域请求数据方法

    以下测试代码使用php,浏览器测试使用IE9,chrome,firefox,safari <!DOCTYPE HTML> <html> <head>     < ...

  7. ajax 的三种使用方法

    第一种 也是最古老的一种方法之一 from 表单直接提交数据到php文件里 action为路径 <form method="post" action="./inde ...

  8. AJax的三种响应

    AJax的响应 1.普通文本方式(字符串) resp.getWriter().print("你好"); 2.JSON格式当要给前台页面传输 集合或者对象时 使用普通文本传输的时St ...

  9. CSS——三种页面引入方法

    目的:为了把样式和内容分开,并且使网页元素更加丰富,引入了CSS CSS页面引入有三种方式: 1)内联式:比较不常用,因为内容和样式仍然在一起,不方便.示例: <!DOCTYPE html> ...

随机推荐

  1. POJ 3132 &amp; ZOJ 2822 Sum of Different Primes(dp)

    题目链接: POJ:id=3132">http://poj.org/problem?id=3132 ZOJ:http://acm.zju.edu.cn/onlinejudge/show ...

  2. Python——管理属性(2)

    __getattr__和__getattribute__ 眼下已经介绍了特性property和描写叙述符来管理特定属性[參考这里],而__getattr__和__getattribute__操作符重载 ...

  3. NoSql中的B-tree、B+tree和LSM-tree 分类: B7_HBASE 2015-03-15 18:27 85人阅读 评论(0) 收藏

    总结: 1.B+树将数据完全排序,读数据时很快,但当要修改数据时,就需要将新入数据下面的数据重新排位,特别是当写入的数据排在较高的位置时,需要大量的移位操作才能完成写入. 2.SLM牺牲部分的读性能, ...

  4. php课程 3-12 回调参数怎么用

    php课程 3-12 回调参数怎么用 一.总结 一句话总结:有时候需要在一个函数中使用另外一个函数,使用回调的话,那么那个函数的几十行代码就不用写了,其实很基础,就是函数名的字符串的替换,超级基础的. ...

  5. 【27.85%】【codeforces 743D】Chloe and pleasant prizes

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  6. SpringMVC上传图片总结(1)---常规方法进行图片上传,使用了MultipartFile、MultipartHttpServletRequest

    原文地址:https://blog.csdn.net/chenchunlin526/article/details/70945877 SpringMVC上传图片总结(1)---常规方法进行图片上传,使 ...

  7. 使用LAMP创建基于wordpress的个从博客网站 分类: B3_LINUX 2014-07-15 16:45 800人阅读 评论(0) 收藏

    参考: http://blog.csdn.net/ck_boss/article/details/27866117 一.mysql配置 1.安装mysql yum install mysql-serv ...

  8. 小梦词典WP8.1应用发布

    这几天一直在做这款应用,今天终于发布了! 小梦词典简介: 小梦词典是一款永久免费无广告的网络词典. 支持英汉单词查询: 支持中,英,法,韩,德,俄,日七国语言翻译,多语言极致体验: 支持生词本记忆,查 ...

  9. STS 3.6.4 SpringMVC 4.1.6 Hibernate 4.3.8 MySQL

    开发环境: Java 1.8 Spring Tool Suite 3.6.4 Spring faramework 4.1.6 Hibernate 4.3.8 Maven 2.9 数据库是MySQL 5 ...

  10. VS(Visual Studio)自动创建的文件格式

    .sln:solution,解决方案文件: .vsxproj:解决方案下的项目文件: .vssettings:环境设置文件, 菜单栏 ⇒ [工具]⇒ [导入和导出设置]⇒ 进行环境设置的导入和导出操作 ...