views:

74

answers:

3
$.ajax({ 
  type: "POST", 
  url: "WebService.asmx/AddVisitor",
  data: "{'fname':'dave', 'lname':'ward'}", 
  contentType: "application/json; charset=utf-8", 
  dataType: "json"
});


I have an Asp.Net WebMethod that takes a firstName, lastName.....as a parameter, how do I send that stuff to that method using the JQuery Ajax method. if i hardcode the above it works without any problem

but if i pass dynamic it fails

var firstName = $("[id$='txtFirstName']");
var lastName = $("[id$='txtLastName']");

//data: "{'firstName':'Chris','lastName':'Brandsma'}"<br>

data: "{'firstname':'" + escape(firstName.val()) + "','lastName':'" + escape(lastName.val()) + "'}",

my WebMethod looks like this

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ToolboxItem(false)]
[System.Web.Script.Services.ScriptService]
public class VisitorWS : System.Web.Services.WebService {
[WebMethod]
public bool AddVisitor(string firstName, string lastName)
{
return true;
}


what wrong here? i have tried with eval and escape none of that works.

Thanks for any help.

A: 

From what I can tell your code to set the data looks fine. The problem must be with getting the values of first name and last name. Have you tried showing the values in an alert after you get them, just to make sure that they have values?

On a possibly unrelated note, you might be interested in this article about the method of selection that you're using...

http://encosia.com/2009/06/09/11-keystrokes-that-made-my-jquery-selector-run-10x-faster/

Hope this helps!

Peter
i check with alert to see what value i am getting and its getting whatever the value i have entered...i am thining it has to do with string instead of passingthis is a test vs 'this is a test' ? may be?
Abu Hamzah
Try this then...var d = "{'firstname':'" + escape(firstName.val()) + "','lastName':'" + escape(lastName.val()) + "'}";alert(d);...and see if you get what you'd expect.
Peter
A: 

I'm not using the WebService stuff anymore, but do you have the [ScriptMethod] attribute as well?

Be sure to check FireBug to see what the actual error returned is. Could be a 404 which is a very different issue.

Chris Brandsma
[WebService(Namespace = "http://tempuri.org/")][WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]<br>[ToolboxItem(false)]<br>[System.Web.Script.Services.ScriptService]<br>public class VisitorWS : System.Web.Services.WebService {<br> this is what i have in my web service page. am i missing something? <br>ps: at my work there is restriction for downloading any software so we are suppose to use only IE
Abu Hamzah
I think ScriptMethod means that you're returning JSON.
Peter
i have added the scriptmethod but no luck - unless i am not doing the correct way. [WebMethod] [ScriptMethod] public bool AddVisitor(string firstName, string lastName) { return true; }
Abu Hamzah
ScriptMethod allows the method to return JSON, and if you look at the method above, he is instructing the web service method to return JSON (that is the last parameter).
Chris Brandsma
Have you looked at the method in the Firebug Console yet? That is the best way to see what is going on.
Chris Brandsma
>>>firebug consolefor security reason we are not allowed to download any new software and it needs esp request for that. and currently i am using IE 7
Abu Hamzah
I think Chris is right, you need the ScriptMethod tag. Your web service should look like this: [WebMethod, ScriptMethod] public bool AddVisitor(string firstName, string lastName) { return true; } You need to seperate WebMethod and ScriptMethod with a comma.
Peter
A: 

i have figured out and here it works without adding scriptmethod to it.

var myData = { 
  "firstName": escape($('#txtFirstName').val()),
  "lastName": escape($('#txtLastName').val())
};

$.ajax({
  // ...  
  data: JSON.stringify(myData),
  // ...
});
Abu Hamzah