简体   繁体   中英

calling ajax server-side method with mappage route c#

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

$(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

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

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM