I'm trying to write username validation using jquery, I'm using jmsajax plugin.I have tested webservice, it work fine. I'm getting error.
Webservice code
[System.Web.Script.Services.ScriptMethod(ResponseFormat = ResponseFormat.Json)]
[WebMethod]
public bool check_username(string uname)
{
DBMOdelDataContext db = new DBMOdelDataContext();
var q = from p in db.users
where p.username == uname
select p;
if (q.Count() == 0)
return false;
else
return true;
}
Jquery code.
$(document).ready(function() {
$(".unamebtn").blur(function() {
$uname = $(this).val();
$.jmsajax({
type: "POST",
url: "Services/MyServices.asmx",
data:"{userName='" + $uname + "'}",
method: "check_username",
dataType: "msjson",
success: function(result) {
$("#msg").html(result);
alert(result);
$("#msg").addClass("notice");
}
});
});
});
The Exception it is throwing.
{"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) at System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeMain (Object o, Type type, JavaScriptSerializer serializer, Boolean throwOnError, Object& convertedObject ) at System.Web.Script.Serialization.ObjectConverter.ConvertObjectToType(Object o, Type type, JavaScriptSerializer serializer) at System.Web.Script.Serialization.JavaScriptSerializer.Deserialize(JavaScriptSerializer serializer, String input, Type type, Int32 depthLimit) at System.Web.Script.Serialization.JavaScriptSerializer .Deserialize[T](String input) at System.Web.Script.Services.RestHandler.GetRawParamsFromPostRequest (HttpContext context, JavaScriptSerializer serializer) at System.Web.Script.Services.RestHandler .GetRawParams(WebServiceMethodData methodData, HttpContext context) at System.Web.Script.Services .RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)","ExceptionType" :"System.InvalidOperationException"}
data: {uname:$uname},
Had to look at the docs actually, jMs is little bit different than normal jQuery. In normal jQuery it would just be:
$.ajax({
type: "POST",
url: "Services/MyServices.asmx/check_username",
data: "{uname:'" + $uname + "'}",
datatype : 'json',
contentType : 'application/json',
success: function(data) {
$("#div").html(String(data));
}
});
Also, please disable GET requests to avoid CSRF security issues.
[ScriptMethod( UseHttpGet = false, ResponseFormat = ResponseFormat.Json )]
have you tried?
$.jmsajax({
...
data: { uname: $uname },
...
});
I had the same problem, and found that I was unknowingly submitting a dictionary object inside of the jQuery call. The error seems to arise when the submitted object type does not map to the parameter expected by your webmethod. If you don't provide the correct identifier for your "data" field in the $.ajax(...) call, you'll receive this error. Make sureyou give an identifier to your data field:
{param_name:"value"}
instead of what I was doing {{field1:"value",field2:value}}
The nested data will cause .net to attempt to deserialize the object as a dictionary. That's okay if your webmethod expects a Dictionary object, but otherwise not so much.
Just a note, and not going too deep, but I had this error when I used ByRef instead of ByVal on the Method Signature! Wasn't looking what I was doing and missed it loads of times looking at the code - needle and haystack thing.