消息:无效的JSON原语:带有Webmethod的ajax jquery方法 [英] Message: Invalid JSON primitive: ajax jquery method with Webmethod

查看:83
本文介绍了消息:无效的JSON原语:带有Webmethod的ajax jquery方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将Data值用作对象文字,而不是如

I am using Data value as object literal, instead of concatenating a String as explained in 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);
  }
})

我的网络方法是这样的:

and my webmethod is like this :

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

但是出现以下错误:

消息:无效的JSON原语:projectSoid

Message: Invalid JSON primitive: projectSoid

推荐答案

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

With your contentType: 'application/json; charset=utf-8' you are claiming that you will send JSON but currently your data property is not holding JSON.

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

You need to transform your data to JSON with the JSON.stringify method:

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

So change your data property to:

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

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

You should note that the JSON.stringify method is not natively supported in older browsers so you may need to provide an implementation with using one of the various libraries like:

Douglas Crockford的 JSON2库.

Douglas Crockford's JSON2 library.

这篇关于消息:无效的JSON原语:带有Webmethod的ajax jquery方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆