Click here to Skip to main content
15,881,559 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi guys.
Admitting that I'm not familiar with client-side coding..
I'm trying to get this ajax call working, but I can't figure it out..

JavaScript
function ShowAlert()
{
  $.ajax(
    {
      type: "POST",
      url: "DownladData.aspx/Alert",
      data: "{}",
      contentType: "application/json; charset=utf-8",
      datatype: "json",
      statusCode: {
        404: function ()
        {
          alert('Method not found!');
        }
      },
      success: function (msg) {
        alert('Everything is ok!');
      },
      error: function (msg) {
        alert('Error!!!')
      }
    });
}


While this is the C# code

C#
[WebMethod]
  public static void Alert()
  {
  }


And this is the button through which I make the ajax call
ASP.NET
<input type="button"  runat="server"  önclick="ShowAlert();" value="Alert" />


This routine doesn't do anithing, but I'd except an alert with the Everything is ok message, instead of an Error!!!.

Using chrome debugger, I get this message:

Failed to load resource: the server responded with a status of 404 (Not Found) http://localhost/AdminPages/DownloadData.aspx/Alert

where Alert() is the WebMethod in the DownloadData.aspx.cs file.

Any suggestion?

Thank you very much!
Posted
Updated 9-Sep-14 22:29pm
v2
Comments
Sinisa Hajnal 10-Sep-14 4:38am    
What does your error msg say? from error: function(msg) try alert (msg) instead of alert("Error")
leotato008 10-Sep-14 4:52am    
I'm getting two alerts: first of all the generic one 'Error!!!' and then the specific 404 one ('Method not found')
Thanks7872 10-Sep-14 4:41am    
Try rebuilding the project again. I don't see any issue as of now.
Sinisa Hajnal 10-Sep-14 5:54am    
You have a typo Downl - a - d instead of Downl -oa -d at least in the code above...

Also, you might add AdminPages to the path if the error persists
url: "AdminPAges/DownladData.aspx/Alert",

Well, I've found the solution to this puzzle.

I'll report what I've discovered to help all of the other people.

My Ajax/Jquery code was ok.

The method must be put in the codebehind of an .aspx page (not sure if it's working even if put behind a master page) and it has to be declared as public.

You can call another method from within the aspx.cs method called via jquery/ajax, as you would do with any other external dll call.

Thx everybody for your help.
 
Share this answer
 
My guess would be that your error handling is wrong. I'm not familiar with statusCode option (maybe it was added recently?)

This is how I would check for the error status:
script
.
.
.
error: function (xhr, status, msg)
(jqXHR.status && xhr.status == 404){
                    alert(xhr.responseText); /* or your custom text*/
               }else{
                   alert("Something else went wrong");
               }



Several variations and even error maps here[^]


If this helps, please accept the solution so other may find it.
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900