JQuery validation using remote messages empty data in webservice

I am using JQuery Validation plugin. I use the remote option to call my web service to check if the company name exists. The webservice only accepts JSON data.

I am passing data to a web service from a company input field to my form like this:

data: "{'company': '" + $ (' # Company '). val () + "'}"

But this always returns blank for the company, so the response is {'company': ''}, that is, valid JSON, but the value of the company input field is missing.

Can anyone shed some light on why I am always getting empty here?

Thanks for the help, Kiran

+2


a source to share


3 answers


The reason is that

data: "{'company': '" + $('#Company').val() + "'}"

evaluated when the page is loaded, not when the remote call is in progress.

Try to declare the rule like this:




remote: function() {  
    var r = {  
        url: "webservice.asmx",  
        type: "POST",  
        contentType: "application/json; charset=utf-8",  
        dataType: "json",  
        data: "{'company': '" + $('#Company').val() + "'}"  
        dataFilter: function(data) { return (JSON.parse(data)).d; }  
       }   
    return r;  
  }

      

If you are using ASP.Net web service a DataFilter is needed because the response will be in the "d" attribute for the json object. The JSON library is required.

"{ d: "true" }"

+5


a source


Try the following:



data: {
    company: $('#Company').val(),
    param: 1  //Second paramenter
              //you can keep adding parameters folloiwng the format 
}

      

0


a source


Try this:

data: function() { return "{'company': '" + $('#Company').val() + "'}"; }

      

This worked for me

0


a source







All Articles