I have the following code, for passing a parameter to a web method, and retrieving a the result in jquery ajax:
<script type="text/javascript">
$(document).ready(function () {
$('#<%=btnSignup.ClientID %>').click(function () {
var dataString = JSON.stringify({
firstName: $("#SignupFirstName").val(),
lastName: $("#SignupLastName").val(),
email: $("#SignupEmail").val(),
password: $("#SignupPassword").val()
});
$.ajax({
type: "POST",
url: "Signup.aspx/Signup",
data: dataString,
contentType: "application/json; charset=utf-8",
dataType: "json",
async: true,
cache: false,
beforeSend: function () {
// some code
},
success: function (result) {
$('#loading').hide();
if (result.hasOwnProperty("d")) { result = result.d; }//and some more code
}
});
return false;
});
});
</script>
and the web method:
[WebMethod]
public static SignupOutput Signup(string firstName, string lastName, string email, string password)
{
// execute some code and return an object for the json
}
The method is not executing, I’m using nice urls. Please help, what is wrong with the code?
Thanks.
I would first start by throwing an
alertinside your click function, just to make sure it’s actually being hit. Assuming that it is…Check out the documentation for /Base. Be sure not to miss the links on the right of that page, they are sort of obscure.
Basically, what I think you’ll need to do is…
Move your WebMethod into a standalone class and remove the
[WebMethod]attribute. I’ve also struggled with returning objects in the past and what I’ve moved to is just building a json-formatted string and returning that. So what you would end up with is something like this:Add configuration to /presentation/config/restExtensions.config
Obviously you’ll need to tweak this a little to match your exact assembly name / namespaces.
Change the ajax call.
Hopefully this will help you. Good luck!