Reputation: 69
Hi everyone I have a web page that is using Jquery to send the data from a dialog to the asp.net method using $.ajax, but it's always giving me a 404 error Web Page not found.
The Ajax is giving this link to the request "Localhost:1395/Login.aspx/sendEmail" (Obtained using firebug), but send Email is the method that should be called in the Login.aspx page.
This is the JQuery code:
$.ajax({
type: 'POST',
url: 'Login.aspx/sendEmail',
data: '{"strEmail":"' + $('#hplForgotDialog').find('input[id$=txtForgotEmail]').val() + '"}',
contentType: "application/json; charset=utf-8",
dataType: "json"
});
Any help with this problem would be really apreciated.
Edit: to demonstrate the error a little more I'll add an image depicting the URL error to witch it tries to connect.
Upvotes: 3
Views: 542
Reputation: 2181
I assume that you using asp.net webforms not mvc. So you have to make the following method in the Login.aspx.cs file (Login class I assume):
[WebMethod()]
[ScriptMethod(ResponseFormat=ResponseFormat.Json)]
public static object sendEmail(string strEmail)
{
return new { emailSent = true};
}
Criterias:
- Method have to be static
- The attributes are required (if you do not wish to return with json, there are other formats)
- The method and the parameters name is from the $.ajax request so if you change you should change that too.
Upvotes: 0
Reputation: 1708
My guess would be, you need to setup routing. You can read about it here: http://msdn.microsoft.com/en-us/library/cc668201.ASPX
Basically, if I'm right (wich I might not be), your routing doesn't find the correct Actions (or whatever they are called in a non-MVC scenario). In Web Forms, you have to setup custom routes in the Global.asax file, in the Application_Start event handler.
Something like this:
protected void Application_Start(object sender, EventArgs e)
{
RegisterRoutes(RouteTable.Routes);
}
public static void RegisterRoutes(RouteCollection routes)
{
routes.MapPageRoute("",
"Category/{action}/{categoryName}",
"~/categoriespage.aspx");
}
With this, you tell the application how to understand URLs, and their parameters. This is the url /Category/param1/param2, everything that matches this pattern, will be directed to the categoriespage.aspx page, what can do whatever it wants with the parameters (call the correct method for example).
Upvotes: 1
Reputation: 172378
Try this:-
$.ajax({
type: "POST",
url: "Login.aspx/sendEmail",
data: '{"strEmail":"' + $('#hplForgotDialog').find('input[id$=txtForgotEmail]').val() + '"}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
// Your code.
}
});
Upvotes: 1