Message: Invalid JSON primitive: ajax jquery method with c# Webmethod

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (299)

我有这样的代码:我使用数据值作为对象文本,而不是连接一个字符串。为什么?

我的代码是:

$.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)
        {}

问题是我犯了这样的错误:

Message: Invalid JSON primitive: projectSoid

提问于
用户回答回答于

客户端JavaScript

 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方法

[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];

    }
用户回答回答于

用你的contentType: 'application/json; charset=utf-8',你说你将发送JSON,但目前你的data属性不包含JSON。

你需要把你的data到JSON的JSON.stringify方法:

所以改变你的data属性:

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

扫码关注云+社区

领取腾讯云代金券

玩转腾讯云 有奖征文活动