【问题标题】:Message: Invalid JSON primitive: ajax jquery method with Webmethod消息:无效的 JSON 原语:带有 Webmethod 的 ajax jquery 方法
【发布时间】:2013-02-23 23:32:45
【问题描述】:

我使用数据值作为对象字面量,而不是像this answer 中解释的那样连接字符串

我的代码如下:

$.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]
public static string SaveClient(string projectSoid, string startDate, 
     string endDate, string clientManager)
{
    ...
}

但我收到以下错误:

消息:无效的 JSON 原语:projectSoid

【问题讨论】:

  • 你需要 JSON.strigify 你的数据:data: JSON.strigify({ "projectSoid": ProjectId, "startDate": StartDate, "endDate": EndDate, "clientManager": ClientManager }),
  • 对我的评论有意见吗?你试过了吗?有用吗?
  • 我不知道 JSON.strigify 是什么?我收到错误提示它不起作用:(
  • 您使用的是哪个浏览器?在旧版浏览器中您可能需要json2
  • Nestor 出现错误,因为 nemesv 拼写错误(错过了 n):JSON.stringify

标签: c# jquery asp.net asp.net-ajax


【解决方案1】:

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

您需要使用JSON.stringify 方法将data 转换为JSON:

因此,将您的 data 属性更改为:

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

您应该注意,旧版浏览器本身不支持 JSON.stringify 方法,因此您可能需要提供使用各种库之一的实现,例如:

道格拉斯·克罗克福德的JSON2 library.

【讨论】:

  • @nemesv:谢谢 Nemev:您的代码中只有一处更正... U 错误地将 stringify 拼写为 stigify...
  • @NestorC :如果它满足您的需求,您可以接受这个答案吗?这样它会对某人有所帮助......
  • 如果不想使用 JSON.stringify,可以去掉内容类型。默认情况下,内容类型为 application/x-www-form-urlencoded; charset=UTF-8(参见api.jquery.com/jquery.ajax)。我更喜欢使用 JSON.stringify 就像@nemesv 指出的那样,但我只想说它不是强制性的。
【解决方案2】:

客户端的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];

    }

【讨论】:

    猜你喜欢
    • 2014-02-01
    • 2016-04-19
    • 1970-01-01
    • 2020-12-29
    • 1970-01-01
    • 2018-01-16
    • 1970-01-01
    • 2015-06-13
    • 1970-01-01
    相关资源
    最近更新 更多