日期:2014-05-17  浏览次数:20747 次

ajax 调用 webservice 返回 object
在 ie上调用的结果是这个样子的  
<?xml version="1.0" encoding="utf-8" ?> 
  <string xmlns="http://tempuri.org/">{"":[[{"id":"1","name":"张三","sex":"男","age":"25","depar":"软件部"},{"id":"2","name":"李四","sex":"女","age":"24","depar":"人事部"},{"id":"3","name":"王五","sex":"男","age":"34","depar":"部门经理"},{"id":"4","name":"刘牛","sex":"女","age":"23","depar":"人事部"},{"id":"5","name":"张三","sex":"男","age":"25","depar":"软件部"}]}</string> 

但是 用ajax调用就返回 object 了 无法解析,javascript 代码
JScript code
 $.ajax({
                type: "POST",
                contentType: "application/json",
                url: "WebService1.asmx/Getstaff",
                data: "{currPage: '1', tabName: 'staff'}",
                dataType: 'json',
                success: function (data) {
                    alert(data);
                }
            });


webservice 代码
C# code


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using DataCall;
using System.Data;
using System.Text;

namespace WebApplication1
{
    /// <summary>
    /// WebService1 的摘要说明
    /// </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 WebService1 : System.Web.Services.WebService
    {
        DAL dal = new DAL();
        [WebMethod]
        /// <summary>
        /// 获取信息
        /// </summary>
        /// <param name="currPage">当前页页码</param>
        /// <param name="showColumn">需要得到的字段</param>
        /// <param name="tabName">需要查看的表名</param>
        /// <param name="strCondition">查询条件</param>
        /// <param name="ascColumn">排序的字段名</param>
        /// <param name="bitOrderType">排序的类型 (0为升序,1为降序)</param>
        /// <param name="pkColumn">主键名称</param>
        /// <param name="pageSize">分页大小</param>
        /// <returns></returns>
        public string Getstaff(int currPage, string tabName)
        {
            DataTable dt = dal.GgtSatff(currPage, "*", tabName, "", "id", 0, "id", 5);
            string json = DataTable2Json(dt);
            return json;
        }
        #region dataTable转换成Json格式
        ///<summary>
        /// dataTable转换成Json格式 
        ///</summary>
        ///<param name="dt"></param>
        ///<returns></returns>
        public static string DataTable2Json(DataTable dt)
        {
            StringBuilder jsonBuilder = new StringBuilder();
            jsonBuilder.Append("{\"");
            jsonBuilder.Append(dt.TableName);
            jsonBuilder.Append("\":[");
            jsonBuilder.Append("[");