2

I'm using the JQuery Validation plugin. I'm using the remote option to make a call to my webservice to check if a company name exists. The webservice only accepts JSON data.

I pass the data to the webservice from the Company Input Field in my Form as follows:

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

But this always returns a blank value for company so the response is {'company':''} i.e. correct JSON but missing the Company Input Field value.

Can anyone shed some light on why I always get a blank value here?

Thanks for the help, Ciaran

Click Ahead
  • 2,782
  • 6
  • 35
  • 60
  • you should post a little more code of your $.ajax request and explain which kind of web service you use. Probably you problem is close to http://stackoverflow.com/questions/2651091? I posted as an answer some different versions of $.ajax request to show problem from the different sides. – Oleg Apr 19 '10 at 22:40

3 Answers3

5

The reason is that

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

is evaluated when the page loads not when te remote call is made.

Try declaring the rule this way:


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 a ASP.Net webservice, dataFilter is needed because the response will be in a "d" called property of the json object. JSON library would be required.

"{ d: "true" }"

Codigo Espagueti
  • 174
  • 2
  • 10
0

Try this:

data: {
    company: $('#Company').val(),
    param: 1  //Second paramenter
              //you can keep adding parameters folloiwng the format 
}
Pablo
  • 5,897
  • 7
  • 34
  • 51
  • Hi Pablo, Thanks for the reply. This works to give me back the value but the format is wrong as it is a querystring instead of JSON i.e. Company=test instead of {Company:test} I wonder if this is a bug with the Validation plugin? – Click Ahead Apr 19 '10 at 19:09
  • Wierd,and where exactly do you see this Company=test? after you parse the json string in PHP? if using json_decode() it should return an associative array like [comapany] => "test", [param] => 1 – Pablo Apr 20 '10 at 04:49
  • Hi Pablo, I use Firebug to debug this. If I set the data parameter as you suggested it is urlencoded with the value. If I set it as I originally suggested it is json encoded but with no value set. I agree this is strange behaviour on the part of the Validation Plugin. I'm vering down the path of using ASP.NET MVC so I don't think this will be a problem here as I can return json data using an action method that returns JSONResult in my Controller. – Click Ahead Apr 21 '10 at 00:42
0

Try this:

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

or better way:

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

This worked for me

mojtaba baghban
  • 111
  • 1
  • 2