3

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.

enter image description here

1
  • the method you try to call, is it a webmethod? Commented Aug 22, 2013 at 8:23

3 Answers 3

1

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).

Sign up to request clarification or add additional context in comments.

2 Comments

can you elaborate a little more I'm using .NET 3.5 and have never worked with routing....
I have edited my answer, to provide a little explanation, but you really have to read and understand the article I have linked. Hopefully this helps, I am working with asp.net mvc 4, so I'm not really much of a help, if this doesn't work.
1

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.
  }
});

1 Comment

Thanks friend but no good I had already used the success clause but that wasn't the problem,sorry but the thing is it's not doing the request since it thinks the sendEmail is a web page not a method nested within the login.aspx page
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.