JQuery调用Webservice和ASP.NET AJax调用Webservice

首先WebService返回的是struct类型,返回的是Class类型同样也能调用。

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.IO;
using System.Data;
using System.Data.OleDb;

namespace LearnAjax
{
    /// <summary>
    
/// WebService2 的摘要说明
    
/// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
     [System.Web.Script.Services.ScriptService]
    public class WebService2 : System.Web.Services.WebService
    {
        public struct ListResult
        {
            public int totleCount;
            public int totlePage;
            public string haoma;
            public DataTable dt;
        }

        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }

        //获取短信信息
        [WebMethod(EnableSession = true)]
        public ListResult GetPagemessages2(string orderid, string searchsql, int pageindex, int pagesize)
        {
            int totleRecord;
            int totlePage;
            ListResult messages = new ListResult();
            DataTable dt = DAL.GetPagemessages3(orderid, searchsql, pageindex, pagesize, out totleRecord, out totlePage);
            messages.dt = dt;
            messages.haoma =dt.Rows[0].ToString();
            messages.totleCount = totleRecord;
            messages.totlePage = totlePage;
            return messages;
        }
    }
}

 前台JQuery调用代码是:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="HC_MDOC.aspx.cs" Inherits="LearnAjax.HC_MDOC" %>

<!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>JQuery方式调用的WebService</title>
    <script src="jquery.js" type="text/javascript"></script>
     <script type="text/javascript">
         

         $(document).ready(
function() {
             $(
'#btn4').click(function() {
                 $.ajax({
                     type: 
"POST",
                     contentType: 
"application/json",
                     url: 
"WebService2.asmx/GetPagemessages2",
                     data: 
"{orderid:'SendTime',searchsql:'',pageindex:1,pagesize:10}",
                     dataType: 
'json',
                     success: 
function(result) {
                         $(result.d).each(
function() {
                         
//alert(this);
                         $('#dictionary').append(this['totleCount'+ " " + this['totlePage'+ " " + this['dt'].rows[1].SendAffairContent);
                             
//alert(result.d.join(" | "));
                         });

                     }
                 });
             });
         }); 

    
</script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <input type="button" id="btn4" value="复合类型" />
    <div id="dictionary"></div>
    </div>
    </form>
</body>
</html>

---------------------------------------------------------

ASP.NET Ajax调用代码是(注意:调用的时候加上命名空间):

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="HC2.aspx.cs" Inherits="LearnAjax.HC2" %>

<!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>ASP.NET Ajax调用Webservice</title>
    <script type="text/javascript">
         
         
var SearchSql = "";//"Cfrom='0' ";
         var orderid = "SendTime"//"SendTime";
        
         
function GetListValue2() {

             LearnAjax.WebService2.GetPagemessages2(orderid, SearchSql, 
110, callbacktable2);
         }


         
function callbacktable2(val) {
             
for (var i = 0; i < val.dt.rows.length; i++) {
                 alert(val.dt.rows[i].SendTime.format(
"yyyy-MM-dd HH:mm:ss"));
             }
         }
         
</script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <input type="button" value="点击" onclick="GetListValue2()" />
        <asp:ScriptManager ID="ScriptManager2" runat="server">
            <Services>
                <asp:ServiceReference Path="~/WebService2.asmx" />
            </Services>
        </asp:ScriptManager>
    </div>
    </form>
</body>
</html>

无论哪种调用都要在Web.config文件加上DataSet,DataTable,DataRow序列化设置。否则会出错。

<configuration>
  <system.web.extensions>
    <scripting>
      <webServices>
        <jsonSerialization>
          <converters>
            <add name="DataSetConverter" type="Microsoft.Web.Preview.Script.Serialization.Converters.DataSetConverter" />
            <add name="DataRowConverter" type="Microsoft.Web.Preview.Script.Serialization.Converters.DataRowConverter" />
            <add name="DataTableConverter" type="Microsoft.Web.Preview.Script.Serialization.Converters.DataTableConverter" />
          </converters>
        </jsonSerialization>
      </webServices>
    </scripting>
  </system.web.extensions>
</configuration>

---------------------------------------------------------------------------------------------------

返回的JSON数据如图:

 

posted @ 2011-11-21 16:05  草珊瑚  阅读(346)  评论(0)    收藏  举报