Click here to Skip to main content
15,867,997 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi friends

I am calling a asmx web services using JQuery with passing parameters as an javascript object that is converted to JSon.

C#
var UpdatePDetails = new Object();
    var FName = $("#txtFirstName").val();
    var LName = $("#txtLastName").val();
    var SName = $("#txtSurName").val();
    var BGroup = $("#txtBloodGroup").val();
    var EMail = $("#txtEMail").val();
    var Mobile = $("#txtMobile").val();
    UpdatePDetails.FirstName = FName;
    UpdatePDetails.LastName = LName;
    UpdatePDetails.SurName = SName;
    UpdatePDetails.EMail = EMail;
    UpdatePDetails.BloodGroup = BGroup;
    UpdatePDetails.Mobile = Mobile;
      var LoginUser = $("#HLU").val();
 var param = '{"ThisPrimaryDetails":"' + JSON.stringify(UpdatePDetails) + '", "UserId" : ' + LoginUser + ' }';


and this is my ajax function

SQL
$.ajax
                    ({
                        type: "POST",
                        contentType: "application/json; charset=utf-8",
                        url: "http://localhost:59415/MyProfile.aspx/UpdatePrimaryDetails",
                        data: param,
                        dataType: "json",
                        success: OnUpdateSuccess,
                        error: OnUpdateFail
                    });
    }
                catch (err)
                {
        alert("Error Occured in Ajax Function  " + err.Message);
                }


and this is my web service code

con = New SqlConnection(constr)
Dim cmd As New SqlCommand()
Dim value As Integer
Try
con.Open()
Query = "Update Users Set FirstName='" + ThisPrimaryDetails.FirstName + "' , LastName='" + ThisPrimaryDetails.LastName + "' , SurName='" + ThisPrimaryDetails.SurName + "' , EMail='" + ThisPrimaryDetails.EMail + "' , BloodGroup='" + ThisPrimaryDetails.BloodGroup + "' , Mobile='" + ThisPrimaryDetails.Mobile + "' where UserId='" + UserId.ToString + "'"
cmd = New SqlCommand(Query, con)
value = cmd.ExecuteNonQuery()
Catch ex As Exception
value = -1
'MsgBox(ex.ToString)
End Try
Return value

I am getting the error as below

SQL
Error Occured in Update Fail
{"Message":"Invalid object passed in, \u0027:\u0027 or \u0027}\u0027 expected. (26): {\"ThisPrimaryDetails\":\"{\"FirstName\":\"John\",\"LastName\":\"Suvera\",\"SurName\":\"Abhramin\",\"EMail\":\"john@gmail.com\",\"BloodGroup\":\"O +Ve\",\"Mobile\":\"845932523\"}\", \"UserId\" : 3 }","StackTrace":" at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeDictionary(Int32 depth)\r\n 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"}
Posted
Updated 19-Aug-12 18:04pm
v2

1 solution

You're trying to build your own JSON object. I suggest you create a class that has the structure you want ( two properties, one is a subclass ) and call JSON.stringify on that instead of trying to build it your self ( although the result looks OK to me, there's clearly an issue with it )
 
Share this answer
 
Comments
Ganesh KP 29-Aug-12 2:40am    
Hi Christian Graus Could you Please give me a sample code regarding what you are saying
Christian Graus 30-Aug-12 12:03pm    
I mean, create a class that defines the structure you're creating on the fly
Ganesh KP 7-Sep-12 8:00am    
Hi Graus I have created own class and then created an object to that. still the same error persists me. I think the web service is unable to take the parameter that is sent from the JQuery. What else can we do in another way.
Christian Graus 7-Sep-12 15:53pm    
The web service does not care where the parameter came from, only that it is correctly formed. Can you call the web service from C# ? If so, look at the message being sent and compare it to the one being sent in this code, and work out where they are different.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900