利用javascript+webservice实现post方法

来源:互联网 发布:网络教学的设计原则 编辑:程序博客网 时间:2024/05/29 12:39

前台

 

 

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="LabPage1._Default" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" ><head runat="server">    <title>无标题页</title></head><script type="text/javascript" language="javascript">    var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");     function callback1()    {        if(xmlhttp.status==200 && xmlhttp.readyState==4)        {            var textresponse=xmlhttp.responseText;            var finddiv1=document.getElementById("div1");            finddiv1.innerText=textresponse;        }    }    function RequestByPostOne(){       var data="name=NanceSun&gender=male";        var URL="http://202.119.43.9/WebService/Service1.asmx/MyPostLink";        xmlhttp.Open("POST",URL, false);         xmlhttp.onreadystatechange=callback1;        xmlhttp.SetRequestHeader ("Content-Type","application/x-www-form-urlencoded");         xmlhttp.Send(data);     }     function callback2()    {        if(xmlhttp.status==200 && xmlhttp.readyState==4)        {            var textresponse=xmlhttp.responseText;            var finddiv1=document.getElementById("div2");            finddiv1.innerText=textresponse;        }    }    function RequestByPostTwo(){         var data="i=30&j=31";        var URL="http://202.119.43.9/WebService/Service1.asmx/MyPostAdd";        xmlhttp.Open("POST",URL, false);         xmlhttp.onreadystatechange=callback2;        xmlhttp.SetRequestHeader ("Content-Type","application/x-www-form-urlencoded");         xmlhttp.Send(data);     } </script><body><form runat="server"><br /><input type="button" value="Button1" onclick="RequestByPostOne()" /><div id="div1">DIVDIVDIVDIVDIVDIVDIVDIVDIV</div><br /><input type="button" value="Button2" onclick="RequestByPostTwo()" /><div id="div2">DIVDIVDIVDIVDIVDIVDIVDIVDIV</div><br /></form></body></html>


 

 

后台:

using System;using System.Collections;using System.ComponentModel;using System.Data;using System.Linq;using System.Web;using System.Web.Services;using System.Web.Services.Protocols;using System.Xml.Linq;using System.IO;using System.Web.Security;using System.Web.UI;using System.Web.UI.HtmlControls;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;namespace WebService{    /// <summary>    /// Service1 的摘要说明    /// </summary>    [WebService(Namespace = "http://tempuri.org/")]    [WebServiceBinding(ConformsTo = WsiProfiles.None)]    [ToolboxItem(false)]    // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。    // [System.Web.Script.Services.ScriptService]    public class Service1 : System.Web.Services.WebService    {        [WebMethod(Description = "第一个函数,返回两个string的拼接", MessageName = "MyPostLink")]        public string MyPostLink(string name, string gender)        {            return name + gender;        }        [WebMethod(Description = "第二个函数,返回两个int型的和", MessageName = "MyPostAdd")]        public int MyPostAdd(int i, int j)        {            return i + j;        }    }}


 

原创粉丝点击