json primitive error with jquery and asp.net

asp.net, jquery, vb.net

Solution

The Exception thrown by system occurs only when the data you are sending is not in correct format, so double check the data before sending.

i think data which you are sending should be like this :-

Senddata={"project_id": project_id,"formstatus": formstatus,........};

and in ajax call :-

 $.ajax({
  type: "POST",
  url: "consoservice.asmx/sendmessage",
  data:  JSON.stringify(Senddata),
............
..............
});

Problem

i am trying to run a send message from jquery ajax but getting following error ``` {"Message":"Invalid JSON primitive: .","StackTrace":" at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializePrimitiveObject() at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeInternal(Int32 depth) at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeDictionary(Int32 depth) at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeInternal(Int32 depth)\r\n at System.Web.Script.Serialization.JavaScriptObjectDeserializer.BasicDeserialize(String input, Int32 depthLimit, JavaScriptSerializer serializer)\r\n at System.Web.Script.Serialization.JavaScriptSerializer.Deserialize(JavaScriptSerializer serializer, String input, Type type, Int32 depthLimit)\r\n at System.Web.Script.Serialization.JavaScriptSerializer.Deserialize[T](String input)\r\n at System.Web.Script.Services.RestHandler.GetRawParamsFromPostRequest(HttpContext context, JavaScriptSerializer serializer)\r\n at System.Web.Script.Services.RestHandler.GetRawParams(WebServiceMethodData methodData, HttpContext context)\r\n at System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)","ExceptionType":"System.ArgumentException"} ``` my jquery code : ``` function sendmessage(){ var project_id = $("#DropDownList1 option:selected").val(); var formstatus = $("#DropDownList4 option:selected").val(); var formcode = $('#FormCText').val(); var uniquecode = getunique(); var to = $('.showto').attr("id"); var cc = getcc(); if ($('#urgent').prop("checked"==true)) { var d = new Date(); var duedate = d.getFullYear() + "/" + (d.getMonth()+1) + "/" + d.getDate(); var urg = 1; } else { var duedate=$('#datepicker').val(); var urg = 0; } if($('#readreceipt').prop("checked"==true)) { var receipt = 1; } else{ var receipt = 0; } var fsub = $('#fsub').val(); var nicE = new nicEditors.findEditor('message'); var message = nicE.getContent(); alert("'project_id':'"+ project_id+"','formstatus':'"+ formstatus+"','formcode':'"+formcode+"','uniquecode':'"+ uniquecode+"','tor':'"+to+"','cc':'"+cc+"','duedate':'"+duedate+"','urg':'"+urg+"','receipt':'"+receipt+"','sub':'"+fsub+"','message':,'"+message+"'"); $(document).timer = setTimeout(function () { alert("now sending values"); $.ajax({ type: "POST", url: "consoservice.asmx/sendmessage", data: "{'project_id':'" + project_id+ "','formstatus':'" + formstatus+ "','formcode':'" + formcode + "','uniquecode':'ghfg','tor':'"+to+"','cc':'"+cc+"','duedate':'"+duedate+"','urg':'"+urg+"','receipt':'"+receipt+"','fsub':'"+fsub+"','message':,'"+message+"'}", contentType: "application/json; charset=utf-8", dataType: "json", success: function(msg) { if (msg.d == "sent") { alert('message sent successfully for approval'); } else{ alert(msg.d); } // Do something interesting here. } }); }, 100); } ``` my service function code ``` <System.Web.Services.WebMethod()> _ <System.Web.Script.Services.ScriptMethod()> _ Public Function sendmessage(ByVal project_id As String, ByVal formstatus As String, ByVal formcode As String, ByVal uniquecode As String, ByVal tor As String, ByVal cc As String, ByVal duedate As String, ByVal urg As String, ByVal receipt As String, ByVal fsub As String, ByVal message As String) As String MsgBox("reached") Dim ip As String = System.Web.HttpContext.Current.Request.UserHostAddress Dim usern As String = CookieUtil.GetEncryptedCookieValue("user_id") Dim returnvalue As String Dim ari As New moduleclass Dim i As Integer Try i = ari.InsertAp_module_message(usern, tor, formcode, cc, formstatus, duedate, receipt, urg, fsub, message, -1, uniquecode, 0, 0, DateTime.Now, ip, 0) Catch ex As Exception MsgBox(ex.Message) End Try If i > 0 Then returnvalue = "sent" Else returnvalue = "not sent" End If Return returnvalue End Function ```

Original source