0

Salam aleykum, i'm trying here to call an ajax server-side method using the mappage route but it always says: POST 404 (Not found)

here is the code c#:

[System.Web.Services.WebMethod]
        public static bool RemovePhotofromAlbum(string list_photos_hotel)
        {
           .....
            return true;
        }

and here the jquery code should like :

 function RemovePhotofromAlbum(list_photos_hotel) {
                $.ajax({
                    type: "POST",
                    url: $(location).attr('pathname') + "/RemovePhotofromAlbum",
                    data: '{list_photos_room_type: "' + list_photos_hotel + '" }',
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (response) {
                        ....
                    },
                    failure: function (response) {
                        alert(response.d);
                    }
                });
            }

Without using the mappage route it's working. but here i want to use the mappage route i know that there is a problem with the URL in the ajax method but i don't know how to fix it. any help would be appreciated. :)

If it's impossible just tell me

2 Answers2

0

$(location).attr('pathname')

This line is used for add attribute in DOM

So if you want to save baseurl,Keep it in web.config, hidden field or any other js file and than use it

Senjuti Mahapatra
  • 2,570
  • 4
  • 27
  • 38
Tauqeer
  • 19
  • 1
0

You should use $(location).attr('href') and not $(location).attr('pathname')

and you have an error with your parameter name it should be 'list_photos_hotel' and not 'list_photos_room_type'

try this :

 function RemovePhotofromAlbum(list_photos_hotel) {
    $.ajax({
        type: "POST",
        url: $(location).attr('href') + "/RemovePhotofromAlbum",
        data: '{list_photos_hotel: "' + list_photos_hotel + '" }',
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (response) {

        },
        failure: function (response) {
            alert(response.d);
        }
    });
}

assuming you run the script from the same aspx page your server method runs.

Edit :

Because you use map route, you get 404. you should pass the physical location.

Your method is in the path : Manage/admin_2/index.aspx :

function RemovePhotofromAlbum(list_photos_hotel) {
$.ajax({
    type: "POST",
    url: "/Manage/admin_2/index.aspx/RemovePhotofromAlbum",
    data: '{list_photos_hotel: "' + list_photos_hotel + '" }',
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function (response) {

    },
    failure: function (response) {
        alert(response.d);
    }
});

}

KD2ND
  • 321
  • 2
  • 15
  • i did what you said i replace the 'pathname' by 'href' .. same issue – Wijlini Abdelmonem Apr 25 '17 at 11:55
  • add console.log($(location).attr('href')) before the ajax call to see what exactly is the url. – KD2ND Apr 25 '17 at 13:18
  • it gives the full path and that didn't resolve my problem – Wijlini Abdelmonem Apr 25 '17 at 15:26
  • because of the routing you obviously get the 404 error. You know where the server method is , so just pass it in the url parameter. see edited answer. – KD2ND Apr 27 '17 at 05:50
  • you want to say that with mappage route will not work ? – Wijlini Abdelmonem Apr 27 '17 at 10:06
  • Use MapPageRoute as you wish. declare it in Global.asax Application_Start method or wherever you decide. BUT for the ajax function, as i edited the answer above, use the physical path. the answer above was tested successfuly with routing. – KD2ND Apr 27 '17 at 12:18
  • yes i understood what you said ... physical path only for ajax fuctions – Wijlini Abdelmonem Apr 27 '17 at 12:30
  • Please post your MapPageRoute setup – KD2ND Apr 27 '17 at 12:50
  • `routes.MapPageRoute( "Dashboard", "dashboard", "~/Manage/admin_2/index.aspx");` this under `Application_Start` witch is under `Global` – Wijlini Abdelmonem Apr 27 '17 at 13:33
  • i see no problem. see edited answer with your exact routing setup. Go to http://[YOUR_HOST]/dashboard and reach the RemovePhotofromAlbum method in Manage/admin_2/index.aspx.cs file – KD2ND Apr 27 '17 at 13:52
  • yes i m doing that right, but the ideal solution we couldn't make it .... thank you for your time :) – Wijlini Abdelmonem Apr 27 '17 at 13:56
  • please i have another question for you: is there any risk (security or ..) in showing the physical path of a page to the client ? – Wijlini Abdelmonem Apr 27 '17 at 14:04
  • Apart from exposing your physical file structure there is no real security risk. Many sites do not use any kind of routing. Having said that, url routing enables to expose clean and seo friendly urls. In your scenario i think you should and can use it. – KD2ND Apr 27 '17 at 18:56