views:

726

answers:

3

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)\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.ObjectConverter.ConvertObjectToType(Object o, Type type, 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 .DeserializeT\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"}

+1  A: 
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 )]
Chad Grant
I have tried both things, but still getting same error.
Sharique
oh sorry, dont return IDictionary, can't return an Interface. Return just Dictionary
Chad Grant
It could be another method that is returning IDictionary. comment out all the other methods in the service. Something is trying to serialize IDictionary and that won't work. Double check you have .net 3.5 SP1 also.
Chad Grant
now getting error:"Message":"Invalid JSON primitive: uname."
Sharique
make sure its {uname: $uname} or "{uname:" + $uname + "}" I have never used the jmsa stuff, just standard jquery
Chad Grant
I'm talking abt default ajax call.
Sharique
even though I have selected answer, I still want to know how to with default ajax call. I tried ur last comment but it is still not working well. Can I select multiple answers as correct?
Sharique
A: 

have you tried?

$.jmsajax({
    ...
    data: { uname: $uname },
    ...
});
neouser99
That's messed up, I posted that a day before you did
Chad Grant
A: 

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.

David Lively