使用jQuery Ajax将参数传递给WebMethod的问题 [英] Problem passing parameter to WebMethod with jQuery Ajax
问题描述
大家好,
如何解决我的错误,
{消息:无法转换类型为\\\'System的对象.String \ u0027键入\\\'System.Collections.Generic.IDictionary`2 [System.String,System.Object] \ u0027,StackTrace:at System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeInternal( System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeMain(Object o,Type type,JavaScriptSerializer serializer,Boolean throwOnError,Object& convertedObject)中的Object o,Type type,JavaScriptSerializer serializer,Boolean throwOnError,Object& convertedObject)\\\\ n )System.Web.Script.Serialization.JavaScriptSerializer.Deserialize中的System.Web.Script.Serialization.JavaScriptSerializer.Deserialize(JavaScriptSerializer序列化程序,字符串输入,类型类型,Int32 depthLimit)\\\\ n中的\\\ System.Web.Script.Services.RestHandler.GetRawParamsFromPostRequest中的[T](字符串输入)\\\\ n(HttpConte在System.Web.Script.Script.Services.RestHandler.ExecuteWebServiceCall上的System.Web.Script.Script.Services.RestHandler.GetRawParams(WebServiceMethodData methodData,HttpContext context)\\\\ n中的xt上下文,JavaScriptSerializer序列化程序)\\\ HttpContext context,WebServiceMethodData methodData),ExceptionType:System.InvalidOperationException}
使用jquery调用web方法,
我的脚本代码如
< 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:$( #n ame)。val(),密码:$( #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)。val(),状态:$(#status)。val(),描述:$(#description)。val(),资格:$(#qualification ).val()};
var data = JSON .stringify(数据);
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':+ qualified +},
// data://{'title':'+ $(#title)。val()+','C.Name':'+ $( #name)。val()+','city':'+ $(#city)。val()+','state':'+ $(#state)。val ()+','dat e':'+ $(#date)。val()+','duration':'+ $(#duration)。val()+','pay':'+ $(#pay)。val()+','industry':'+ $(#industry)。val()+','type':'+ $(#type ).val()+','status':'+ $(#status)。val()+','description':'+ $(#description)。val()+ ','资格':'+ $(#qualification)。val()+'},
contentType: application / json; charset = utf-8,
processData: false ,
dataType: json,
成功:功能(数据){
alert(' 成功') ;
},
错误: function (数据,状态,错误){
alert(data.responseText);
}
});
});
});
< / script>
我的HTML代码,
< 表 width = 100% border = 0 cellspacing = 0 cellpadding = 0 类 = form3 > ;
< tr >
< td width = 25% align = left valign = middle >
USERNAME
< / td >
< td width = 75% align = left valign = middle >
< span class =code-keyword>< input id = name type = text class = textbox2 名称 = 名称 大小 = 30 / >
< ; / td >
< / tr >
< tr >
< td align = left valign = mid >
PASSWORD
< / td >
< td align = left valign = middle >
< input id = 密码 类型 = text class = textbox2 name = name size = 30 / >
< / td >
< / tr >
< tr >
< td align = left valign = 中间 >
CLIENTID
< / td >
< t d align = left valign = middle >
< 输入 id = clientid 类型 = text class = textbox2 name = 名称 size = 30 / >
< / td >
< / tr >
< tr >
< td >
< 输入 id = btnok 名称 = 提交 type = 按钮 value = 提交 类 = btn2 border = 0 / >
< / td >
< / tr >
< / table >
我的网络服务代码是,
[WebMethod, ScriptMethod(ResponseFormat = ResponseFormat.Json,UseHttpGet = false )]
public string SETusers( string username, string 密码, 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 ];
参数[ 0 ] = new SqlParameter( @ NAME,SqlDbType.VarChar, 50 );
参数[ 0 ]。Value = username;
参数[ 1 ] = new SqlParameter( @ PASSWORD,SqlDbType.VarChar, 50 );参数[ 1 ]。值=密码;
参数[ 2 ] = new SqlParameter( @ CLIENT_ID,SqlDbType.Int);
参数[ 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;
// 字典< object,object> resultMain = new Dictionary< object,object>();
List< object> resultMain = new List< object>();
foreach (DataRow rs in ds.Tables [ 0 ]。行)
{
字典<字符串,> 结果= new Dictionary< string,> (); foreach (DataColumn dc in ds.Tables [ 0 ]。列)
{
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;
}
最后
{
CloseConnection1();
}
}
( document ).ready( function (){
( < span class =code-string>#btnok)。click( function (){
alert(' ');
var username =
( #name)。val();
var password =
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();
}
}
(document).ready(function () {
("#btnok").click(function () { alert(''); var username =
("#name").val(); var password =
这篇关于使用jQuery Ajax将参数传递给WebMethod的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!