2013-03-08 15 views
17

我有这样的代码:我使用数据值作为对象字面值,而不是连接字符串。为什么? see here消息:无效的JSON基元:使用c#的jQuery jQuery方法Webmethod

我的代码是这样的: -

$.ajax({ 
        url: "../Member/Home.aspx/SaveClient", 
        type: "POST", 
        async: false, 
        dataType: 'json', 
        contentType: 'application/json; charset=utf-8', 
        data: { 
         "projectSoid": ProjectId, 
         "startDate": StartDate, 
         "endDate": EndDate, 
         "clientManager": ClientManager 
        }, 
        success: function (response) { 
         if (response.d != "") { 

         } 
        }, 
        error: function (response) { 
         var r = jQuery.parseJSON(response.responseText); 
         alert("Message: " + r.Message); 
         alert("StackTrace: " + r.StackTrace); 
         alert("ExceptionType: " + r.ExceptionType); 
        } 
       }) 

和的webmethod是这样的:

[WebMethod] 
     public static string SaveClient(string projectSoid, string startDate, string endDate, string clientManager) 
     {} 

问题是我得到的错误是这样的:

消息:无效的JSON原始:projectSoid

+6

您需要JSON.strigify数据:'数据:JSON.strigify({ “ projectSid“:ProjectId, ”startDate“:StartDate, ”endDate“:EndDate, ”clientManager“:Clien tManager }),' – nemesv 2013-03-08 12:18:33

+0

对我的评论发表了评论?你试过了吗?它有效吗? – nemesv 2013-03-08 13:30:59

+0

我不知道什么是JSON.strigify?我有错误,它不是功能:( – 2013-03-08 13:33:20

回答

36

随着contentType: 'application/json; charset=utf-8'你声称你会发送JSON,但目前你的data属性没有持有JSON。

您需要将dataJSON.stringify方法转换成JSON:

所以你data属性更改为:

data: JSON.stringify({ 
    "projectSoid": ProjectId, 
    "startDate": StartDate, 
    "endDate": EndDate, 
    "clientManager": ClientManager 
}), 

你应该注意的是,JSON.stringify方法本身并不在旧的浏览器,因此支持您可能需要使用以下各种库之一提供实施:

Douglas Crockford's JSON2 library.

+0

好吧,谢谢我会尝试:) – 2013-03-08 14:02:11

+0

@nemesv:谢谢Nemev:只是在你的代码更正... U错误地拼写stringify作为strigify ... – Saravanan 2013-05-30 04:38:58

+0

@NestorC:你能接受如果满足您的需求,这是答案吗?所以这对别人有帮助...... – Saravanan 2013-05-30 04:45:09

1

在客户端

var items = [{ projectSoid: ProjectId, startDate: StartDate, endDate: EndDate, clientManager: ClientManager }]; 


        $.ajax({ 
         url: '"../Member/Home.aspx/SaveClient', 
         type: "POST", 
         data: JSON.stringify({ items: items }), 

         //data: JSON.stringify("{DocKey : '" + DocKey + "',highlightText: '" + JSON.stringify(text) + "',pageNo: '" + pgNo + "',left: '" + left + "',top: '" + top + "',width: '" + width + "',height: '" + height + "'}"), 

         //data: "{DocKey\":\""+ DocKey+"\",\"highlightText\":\""+ text +"\",\"pageNo\":\""+pgNo+"\",\"left\":\""+left+"\",\"top\":\""+top+",\"width\":\""+width+"\",\"height\":\""+ height +"}}", 
         // data: "{DocKey : '" + DocKey + "',highlightText: '" + text + "',pageNo: '" + pgNo + "',left: '" + left + "',top: '" + top + "',width: '" + width + "',height: '" + height + "'}", 
         contentType: "application/json; charset=utf-8", 
         dataType: "json", 
         beforeSend: function() { 
          alert("Start!!! "); 
         }, 
         success: function (data) { 
          alert("Save data Successfully"); 
         }, 
         failure: function (msg) { alert("Sorry!!! "); evt.obj.deleteObject(); }, 
         async: false 

        }); 

Web方法的JavaScript代码在背后

[WebMethod]  
public static string SaveClient(object items)  { 

    List<object> lstItems = new  JavaScriptSerializer().ConvertToType<List<object>>(items); 

    Dictionary<string, object> dic = (Dictionary<string, object>)lstItems[0]; 

    }