Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Ajax jQuery WebService
hi all,
how to solve my error,
 
{"Message":"Cannot convert object of type \u0027System.String\u0027 to type \u0027System.Collections.Generic.IDictionary`2[System.String,System.Object]\u0027","StackTrace":" at System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeInternal(Object o, Type type, JavaScriptSerializer serializer, Boolean throwOnError, Object& convertedObject)\r\n at System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeMain(Object o, Type type, JavaScriptSerializer serializer, Boolean throwOnError, Object& convertedObject)\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.InvalidOperationException"}
 
calling web method using jquery,
my script code like
<script language="javascript" type="text/javascript">
 
         $(document).ready(function () {
 

             $("#btnok").click(function () {
                 alert('');
                 var username = $("#name").val();
                 var password = $("#password").val();
                 var clientid = $("#clientid").val();
                 var data = { username: $("#name").val(), password: $("#password").val(), clientid: $("#clientid").val() };
 
                 //  var data = { title:$("#title").val(),name: $("#name").val(), city: $("#city").val(), state: $("#state").val(), date: $("#date").val(),duration:$("#duration").val(),pay:$("#pay").val(),type: $("#type").val(),industry:$("#industry").val(),status:$("#status").val(),description:$("#description").val(),qualification: $("#qualification").val() };
                 var data = JSON.stringify(data);
                 alert(data);
                 $.ajax({
                     type: "POST",
                     url: "http://localhost:3658/HostingService.asmx/SETusers",
                     data: JSON.stringify(data), //"{'title':" + title + ",'C.Name':" + name + ",'city':" + city + ",'state':" + state + ",'date':" + date + ",'duration':" + duratin + ",'pay':" + pay + ",'industry':" + industry + ",'type':" + type + ",'status':" + status + ",'description':" + description + ",'qualification':" + qualification + "}",
                     //     data: // "{'title':'" + $("#title").val() + "','C.Name':'" + $("#name").val() + "','city':'" + $("#city").val() + "','state':'" + $("#state").val() + "','date':'" + $("#date").val() + "','duration':'" + $("#duration").val() + "','pay':'" + $("#pay").val() + "','industry':'" + $("#industry").val() + "','type':'" + $("#type").val() + "','status':'" + $("#status").val() + "','description':'" + $("#description").val() + "','qualification':'" + $("#qualification").val() + "'}",
                     contentType: "application/json;charset=utf-8",
                     processData: false,
                     dataType: "json",
                     success: function (data) {
                         alert('Success');
                     },
                     error: function (data, status, error) {
                         alert(data.responseText);
 
                     }
                 });
 
                  });
        });
       </script>
 
my html code,
<table width="100%" border="0" cellspacing="0" cellpadding="0" class="form3">
        <tr>
            <td width="25%" align="left" valign="middle">
                USERNAME
            </td>
            <td width="75%" align="left" valign="middle">
                <input id="name" type="text" class="textbox2" name="name" size="30" />
            </td>
        </tr>
        <tr>
            <td align="left" valign="middle">
               PASSWORD
            </td>
            <td align="left" valign="middle">
                <input id="password" type="text" class="textbox2" name="name" size="30" />
            </td>
        </tr>
        <tr>
            <td align="left" valign="middle">
               CLIENTID
            </td>
            <td align="left" valign="middle">
                <input id="clientid" type="text" class="textbox2" name="name" size="30" />
            </td>
        </tr>
        <tr>
        <td>
 <input id="btnok" name="Submit" type="button" value="Submit" class="btn2" border="0" />
        </td>
        </tr>
        </table>
 
my web services code is,
[WebMethod, ScriptMethod(ResponseFormat = ResponseFormat.Json,UseHttpGet=false)]
        public string SETusers(string username, string password,string clientid)
        {  DataSet ds = new DataSet();
            try
            {
                SqlDataAdapter adp = new SqlDataAdapter();
                adp.SelectCommand = new SqlCommand();
                adp.SelectCommand.CommandType = CommandType.StoredProcedure;
                adp.SelectCommand.Connection = (SqlConnection)OpenConnection1();
                adp.SelectCommand.CommandText = "SP_USERDETAILS_SET";
                SqlParameter[] Parameters = new SqlParameter[3];
                Parameters[0] = new SqlParameter("@NAME", SqlDbType.VarChar,50);
                Parameters[0].Value = username;
                Parameters[1] = new SqlParameter("@PASSWORD", SqlDbType.VarChar, 50);                Parameters[1].Value = password;
                Parameters[2] = new SqlParameter("@CLIENT_ID", SqlDbType.Int);
                Parameters[2].Value =Convert.ToInt32(clientid);              
                adp.SelectCommand.Parameters.AddRange(Parameters);
                adp.Fill(ds);
                string[][] JaggedArray = new string[ds.Tables[0].Rows.Count][];
                //int i = 0;
                //Dictionary<object, object> resultMain = new Dictionary<object, object>();
                List<object> resultMain = new List<object>();
                foreach (DataRow rs in ds.Tables[0].Rows)
                {
                    Dictionary<string,> result = new Dictionary<string,>();                    foreach (DataColumn dc in ds.Tables[0].Columns)
                    {
                        result.Add(dc.ColumnName, rs[dc].ToString());
                    }
                    resultMain.Add(result);
                    //i = i + 1;
                }
                JavaScriptSerializer js = new JavaScriptSerializer();
                string strJSON = js.Serialize(resultMain);
                return strJSON;
            }
            catch
            {
                CloseConnection1();
                //return ds.GetXml();
                return "-1";
            }
            finally
            {
                CloseConnection1();
            }
        }
Posted 4-Jan-13 0:10am
Edited 4-Jan-13 0:22am
v2
Comments
Member 9581488 at 4-Jan-13 9:41am
   
Please Refer following link.
It might help you.
 
http://elegantcode.com/2009/02/21/javascript-arrays-via-jquery-ajax-to-an-aspnet-webmethod/
 
I am not so sure but giving you suggestion.
 
var data = { username: username, password: password , clientid: clientid };
Try using above line. and check that variables for username password and clientid has some values.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Pass the parameters carefully
 
var params = '{name: "' + $("#<%=txtUserName.ClientID%>")[0].value + '" , dob:18 }';
 

or
fetch values into variable
 
var params = '{GroupId: "' + groupId + '" , TeamId:"' + teamId + '" }';
 
And note that
no overloaded methods are allowed on the server side.
 
for this on server side it must as follows
 
[System.Web.Services.WebMethod]
       public static string SetActionRequiredImage(string GroupId, string TeamId)
       {
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Verify with the code below but all looks fine to me, make sure your server code has no error. Or you will see the error message from 'alert(thrownError)' hope this helps
 
$("#btnok").click(function () {
	var username = $("#name").val();
    var password = $("#password").val();
    var clientid = $("#clientid").val();
    var data = { username: $("#name").val(), password: $("#password").val(), clientid: $("#clientid").val()};
	
	var json_data = JSON.stringify(data);
	
	//ajax call
	$.ajax({
    	type: "POST",
    	url: "http://localhost:3658/HostingService.asmx/SETusers",
    	data: json_data,
    	contentType: "application/json; charset=utf-8",
    	dataType: "json",
    	success: function(data) {
        	alert(data.d);
    	},
    	error: function(xhr, ajaxOptions, thrownError){
        	  alert(thrownError);
    	}
	}); //ajax call end

}); //btnok end
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

What about converting
Dictionary<string,> result = new Dictionary<string,>(); 
to
Dictionary<string,string> result = new Dictionary<string,string>(); 
?
  Permalink  

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

  Print Answers RSS
0 Schatak 394
1 OriginalGriff 300
2 Abhijit Ghosh (Subho) 125
3 _Amy 115
4 Andreas Gieriet 100
0 OriginalGriff 7,107
1 Sergey Alexandrovich Kryukov 5,623
2 Maciej Los 3,504
3 Peter Leow 3,373
4 DamithSL 2,505


Advertise | Privacy | Mobile
Web01 | 2.8.140721.1 | Last Updated 14 Aug 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100