20

I am using Data value as object literal, instead of concatenating a String as explained in this answer

My code is the following:

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

But I get the following error:

Message: Invalid JSON primitive: projectSoid

8
  • 6
    You need to JSON.strigify your data: data: JSON.strigify({ "projectSoid": ProjectId, "startDate": StartDate, "endDate": EndDate, "clientManager": ClientManager }), Commented Mar 8, 2013 at 12:18
  • Any comment on my comment? Have you tried it? Does it worked? Commented Mar 8, 2013 at 13:30
  • i do not know what is JSON.strigify? i got error that it is not function :( Commented Mar 8, 2013 at 13:33
  • 1
    Which browser are you using? You may need the json2 in older browsers Commented Mar 8, 2013 at 13:37
  • 2
    Nestor got a error because nemesv typo error (missed a n): JSON.stringify Commented Jul 3, 2014 at 11:53

2 Answers 2

40

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.

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

So change your data property to:

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

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's JSON2 library.

Sign up to request clarification or add additional context in comments.

3 Comments

@nemesv: Thanks Nemev:Just one correction in your code... U wrongly spelled stringify as strigify...
@NestorC : can you accept this as answer if it meets your needs? So that it will help to somebody...
If you don't want to use JSON.stringify, you can remove the content type. By default, content type is application/x-www-form-urlencoded; charset=UTF-8 (see api.jquery.com/jquery.ajax). I prefer to use JSON.stringify just like @nemesv pointed out but I just wanted to say that it is not mandatory.
2

Javascript at Client side

 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 Method at Code behind

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

    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.