首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >消息:无效的JSON原语:带有Webmethod的ajax jquery方法

消息:无效的JSON原语:带有Webmethod的ajax jquery方法
EN

Stack Overflow用户
提问于 2013-03-08 20:10:31
回答 2查看 35.2K关注 0票数 20

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

我的代码如下:

代码语言:javascript
复制
$.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是这样的:

代码语言:javascript
复制
[WebMethod]
public static string SaveClient(string projectSoid, string startDate, 
     string endDate, string clientManager)
{
    ...
}

但我得到以下错误:

消息:无效的JSON原语: projectSoid

EN

回答 2

Stack Overflow用户

发布于 2013-03-08 21:49:47

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

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

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

代码语言:javascript
复制
data: JSON.stringify({
    "projectSoid": ProjectId,
    "startDate": StartDate,
    "endDate": EndDate,
    "clientManager": ClientManager
}),

您应该注意到,较旧的浏览器本身并不支持JSON.stringify方法,因此您可能需要使用以下各种库中的一个来提供实现:

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

票数 40
EN

Stack Overflow用户

发布于 2016-01-12 18:59:05

客户端上的Javascript

代码语言: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方法

代码语言: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];

    }
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15293717

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档