jQuery 调用WebService 返回的json数据报错

function SetContent(id,value){
               $('#divTitle').html(value);              
              $('#divContent').html("<img src='/help/style/loading2.gif' />");
                  $.ajax({
                    type: 'POST',
                    contentType: "application/json;utf-8", //WebService 会返回Json类型                        
                    url: '/common/Services/WSHelp.asmx/GetContent',                      
                     data:"{id:\""+id+"\"}",
                    dataType: 'json',
                    success: function(data) {

                       $('#divContent').html(data);
                    }
                });  
            }

上面怎么也返回不了正确的数据

改为var o = eval('(' + data + ')');  
$('#divContent').html(data);
提示我缺少'[',去下载查看Request Headers去,找了好久没有找到,不小心到没剑圣域

http://www.cnblogs.com/regedit/archive/2008/03/04/1089948.html

看了一堆无效,只是有一个地方让我的心顿了一下,因为我调试返回的json数据的格式是这样的

data

{...}

d:中华人民共和国,中华人民共和国,中华人民共和国;

 

里面也有一个d,呵呵,改为data.d试试,没有想到就成功了。

 

下面是成功的例子:

function SetContent(id,value){
               $('#divTitle').html(value);              
              $('#divContent').html("<img src='/help/style/loading2.gif' />");
                  $.ajax({
                    type: 'POST',
                    contentType: "application/json;utf-8", //WebService 会返回Json类型                        
                    url: '/common/Services/WSHelp.asmx/GetContent',                      
                     data:"{id:\""+id+"\"}",
                    dataType: 'json',
                    success: function(data) {

                       $('#divContent').html(data.d);
                    }
                });  
            }

 

其它几招,也许没有用,关键时候你还是可以试试

var json = eval('(' + msg + ')');

 

using System;
using System.Data;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.ComponentModel;
using Pay.SQLServer;
using System.Web.Script.Services;
using System.IO;

namespace Pay.common.Services
{
    /// <summary>
    /// Account 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [ToolboxItem(false)]
    [System.Web.Script.Services.ScriptService]
    public class WSHelp : System.Web.Services.WebService
    {

        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }
        [WebMethod]
        [ScriptMethod]
        public string GetContent(string id)
        {
            return Pay.Utility.HelperManage.Instance.Sel_admin_Question(id);
        }      
    }
}

 

    <httpHandlers>
      <remove path="*.asmx" verb="*"/>
      <add path="*.asmx" verb="*" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" validate="false"/>
      <add path="*_AppService.axd" verb="*" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" validate="false"/>
      <add path="ScriptResource.axd" verb="GET,HEAD" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" validate="false"/>
    </httpHandlers>
posted @ 2010-01-21 18:08  月光小提琴  阅读(1179)  评论(0编辑  收藏  举报