0
votes

I also called the LoadLanguage, LoadLanguageSelect, LoadVideoLogLay and LoadImage javascript methods in javascript's pageload method. When the page loads in this way, the actions in those methods will work during page loading. But I get this error when posting javascript methods to asp.net cs side.

My javascript's pageload method

        function PageLoad() {
            LoadLanguages(1);
            LoadLanguageList();
            LoadImageGallery();
            LoadVideoGallery();
        }

And my methods in Javascript

 function LoadLanguageList() {
            $.ajax({
                url: "Edit.aspx/LoadLanguageSelect",
                dataType: "json",
                data: "{}",
                contentType: "application/json; charset=utf-8",
                method: "POST",
                success: function (result) {

                    var xmlDoc = $.parseXML(result.d);
                    var xml = $(xmlDoc);
                    var languages = xml.find("Table");
                    $('#select_AddLang').empty();
                    if (languages.length > 1)
                        $('#select_AddLang').append($("<option></option>").attr("value", 0).text("Choose a language"));
                    else if (languages.length == 0)//All languages has already been added.
                        $('#select_AddLang').css("display", "none");

                    languages.each(function () {
                        var language = $(this);
                        $('#select_AddLang').append($("<option></option>").attr("value", language.find("Ix").text()).text(language.find("Name").text()));
                    });
                },
                error: function (error) {
                    alert(error.d);
                }
            });
        }

        function LoadLanguages(LangIx) {
            $.ajax({
                url: "edit.aspx/LoadLanguages",
                dataType: "json",
                data: "{}",
                contentType: "application/json; charset=utf-8",
                method: "POST",
                success: function (result) {

                    var xmlDoc = $.parseXML(result.d);
                    var xml = $(xmlDoc);
                    var languages = xml.find("Table");
                    $('#languageList').empty();
                    languages.each(function(){
                        var language=$(this);
                        if (LangIx == language.find('Ix').text()) {
                            $('#languageList').append('<li style="list-style:none;display:inline;padding-right:10px;cursor: pointer;"><a id="hypr_lang' + language.find('Ix').text() + '" onmouseover="" style="color:#DF501C;" onclick="ChangeLanguage(' + language.find('Ix').text() + ')"></a></li>');
                            $('#hypr_lang' + language.find('Ix').text() + '').html(language.find('Name').html());
                        }
                        else {
                            $('#languageList').append('<li style="list-style:none;display:inline;padding-right:10px;cursor: pointer;"><a id="hypr_lang' + language.find('Ix').text() + '" onmouseover="" onclick="ChangeLanguage(' + language.find('Ix').text() + ')"></a></li>');
                            $('#hypr_lang' + language.find('Ix').text() + '').html(language.find('Name').html());
                        }
                    });
                },
                error: function (error) {
                    alert(error.d);
                }
            });
        }

function LoadImageGallery() {
            $.ajax({
                url: "Edit.aspx/LoadImageGallery",
                dataType: "json",
                data: "{}",
                contentType: "application/json; charset=utf-8",
                method: "POST",
                success: function (result) {
                    var xmlDoc = $.parseXML(result.d);
                    var xml = $(xmlDoc);
                    var pictures = xml.find("Table");
                    $('#imagesList').empty();
                    var x = 10;
                    pictures.each(function () {
                        var picture = $(this);
                        $('#imagesList').append('<li style="list-style:none;display:inline-block;">' +
                            '<span id="frk' + x + '" class="frk" style="width:132px;text-align:right;" onmouseover="ShowSpan(' + x + ');" onmouseout="HideSpan(' + x + ');">' +
                            '<button type="button" class="glyphicon glyphicon-remove picEditBtn" title="Delete" style="border:none;"' +
                            'onmouseover="ShowSpan(' + x + ');" onmouseout="HideSpan(' + x + ');" onclick="DeleteGalleryPhoto(\'' + picture.find("PictureUrl").text() + '\');"></button></span>' +
                            '<a class="example-image-link" href="/Content/Upload/Images/Gallery/' + picture.find("PictureUrl").text() + '" data-lightbox="example-set">' +
                            '<img style="width:132px;height:132px;" onmouseover="ShowSpan(' + x + ');" onmouseout="HideSpan(' + x + ');" src="/Content/Upload/Images/Gallery/' +
                            picture.find("PictureUrl").text() + '" /></a>' + '</li>');

                        x = x + 1;
                    });
                },
                error: function (error) {
                    alert(error.d);
                }
            });
        }

function LoadVideoGallery() {
            $.ajax({
                url: "edit.aspx/LoadVideoGallery",
                dataType: "json",
                data: "{}",
                contentType: "application/json; charset=utf-8",
                method: "POST",
                success: function (result) {
                    var xmlDoc = $.parseXML(result.d);
                    var xml = $(xmlDoc);
                    var pictures = xml.find("Table");
                    $('#videoList').empty();
                    var x = 10000;
                    pictures.each(function () {
                        var picture = $(this);
                        var url = picture.find("VideoUrl").text();
                        $('#videoList').append('<li style="list-style:none;display:inline-block;">' +
                            '<span id="frk' + x + '" class="frk" style="width:528px;text-align:right;" onmouseover="ShowSpan(' + x + ');" onmouseout="HideSpan(' + x + ');">' +
                            '<button type="button" class="glyphicon glyphicon-remove picEditBtn" title="Delete" style="border:none;"' +
                            'onmouseover="ShowSpan(' + x + ');" onmouseout="HideSpan(' + x + ');" onclick="DeleteGalleryVideo(\'' + url + '\');"></button></span>' +
                            '<video style="width:528px;height:264px;" onmouseover="ShowSpan(' + x + ');" onmouseout="HideSpan(' + x + ');" controls>' +
                             '<source src="/Content/Upload/Images/Gallery/' + url + '" type="video/webm">' +
                            'Your browser does not support the video tag.</video></li>');

                        x = x + 1;
                    });
                },
                error: function (error) {
                    alert(error.d);
                }
            });
        }
[WebMethod]
    public static string LoadLanguages()
    {
        string result=null;
        Edit page = new Edit();
        if (LoadType == "Agency")
        {
            result = page.sqlOp.FillDataSet("SELECT L.Ix,L.Name FROM CONTENT C INNER JOIN [LANGUAGE] L ON L.Ix=C.LangIx WHERE C.AgencyIx=@Ix ORDER BY C.LangIx", page.sqlConnection, CommandType.Text, 5,
                page.sqlParameters.Add("@Ix", SqlDbType.BigInt, Ix)).GetXml();
        }
        if (LoadType == "Tour")
        {
            result = page.sqlOp.FillDataSet("SELECT L.Ix,L.Name FROM CONTENT C INNER JOIN [LANGUAGE] L ON L.Ix=C.LangIx WHERE C.TourIx=@Ix ORDER BY C.LangIx", page.sqlConnection, CommandType.Text, 5,
                page.sqlParameters.Add("@Ix", SqlDbType.BigInt, Ix)).GetXml();
        }
        if (LoadType == "Guide")
        {
            result = page.sqlOp.FillDataSet("SELECT L.Ix,L.Name FROM CONTENT C INNER JOIN [LANGUAGE] L ON L.Ix=C.LangIx WHERE C.GuideIx=@Ix ORDER BY C.LangIx", page.sqlConnection, CommandType.Text, 5,
                page.sqlParameters.Add("@Ix", SqlDbType.BigInt, Ix)).GetXml();
        }

        return result;
    }


[WebMethod]
    public static string LoadLanguageSelect()
    {
        Edit page = new Edit();
        string result = null;
        if (LoadType == "Agency")
        {
            String strQuery = "SELECT L.Ix,L.Name FROM [LANGUAGE] L INNER JOIN CONTENT C ON C.LangIx=L.Ix WHERE C.AgencyIx=@Ix";
            result = page.sqlOp.FillDataSet(strQuery, page.sqlConnection, CommandType.Text, 5,
                page.sqlParameters.Add("@Ix", SqlDbType.BigInt, Ix)).GetXml();
        }
        if (LoadType == "Tour")
        {
            String strQuery = "SELECT L.Ix,L.Name FROM [LANGUAGE] L INNER JOIN CONTENT C ON C.LangIx=L.Ix WHERE C.TourIx=@Ix";
            result = page.sqlOp.FillDataSet(strQuery, page.sqlConnection, CommandType.Text, 5,
                page.sqlParameters.Add("@Ix", SqlDbType.BigInt, Ix)).GetXml();
        }
        if (LoadType == "Guide")
        {
            String strQuery = "SELECT L.Ix,L.Name FROM [LANGUAGE] L INNER JOIN CONTENT C ON C.LangIx=L.Ix WHERE C.GuideIx=@Ix";
            result = page.sqlOp.FillDataSet(strQuery, page.sqlConnection, CommandType.Text, 5,
                page.sqlParameters.Add("@Ix", SqlDbType.BigInt, Ix)).GetXml();
        }
        return result;
    }




[WebMethod]
    public static string LoadVideoGallery()
    {
        string result = null;
        if (LoadType == "Agency")
        {
            Edit page = new Edit();
            result = page.sqlOp.FillDataSet("SELECT VideoUrl FROM GALLERY WHERE AgencyIx=@Ix AND VideoUrl IS NOT NULL",
                page.sqlConnection, CommandType.Text, 5, page.sqlParameters.Add("@Ix", SqlDbType.BigInt, Ix)).GetXml();
        }
        else if (LoadType == "Tour")
        {
            Edit page = new Edit();
            result = page.sqlOp.FillDataSet("SELECT VideoUrl FROM GALLERY WHERE TourIx=@Ix AND VideoUrl IS NOT NULL",
                page.sqlConnection, CommandType.Text, 5, page.sqlParameters.Add("@Ix", SqlDbType.BigInt, Ix)).GetXml();
        }
        else if (LoadType == "Guide")
        {
            Edit page = new Edit();
            result = page.sqlOp.FillDataSet("SELECT VideoUrl FROM GALLERY WHERE GuideIx=@Ix AND VideoUrl IS NOT NULL",
                page.sqlConnection, CommandType.Text, 5, page.sqlParameters.Add("@Ix", SqlDbType.BigInt, Ix)).GetXml();
        }
        return result;
    }



        [WebMethod]
    public static string LoadImageGallery()
    {
        string pictureUrl=null;
        if (LoadType == "Agency")
        {
            Edit page = new Edit();
            pictureUrl = page.sqlOp.FillDataSet("SELECT PictureUrl FROM GALLERY WHERE AgencyIx=@Ix AND PictureUrl IS NOT NULL",
                page.sqlConnection, CommandType.Text, 5, page.sqlParameters.Add("@Ix", SqlDbType.BigInt, Ix)).GetXml();
        }
        else if (LoadType == "Tour")
        {
            Edit page = new Edit();
            pictureUrl = page.sqlOp.FillDataSet("SELECT PictureUrl FROM GALLERY WHERE TourIx=@Ix AND PictureUrl IS NOT NULL",
                page.sqlConnection, CommandType.Text, 5, page.sqlParameters.Add("@Ix", SqlDbType.BigInt, Ix)).GetXml();
        }
        else if (LoadType == "Guide")
        {
            Edit page = new Edit();
            pictureUrl = page.sqlOp.FillDataSet("SELECT PictureUrl FROM GALLERY WHERE GuideIx=@Ix AND PictureUrl IS NOT NULL",
                page.sqlConnection, CommandType.Text, 5, page.sqlParameters.Add("@Ix", SqlDbType.BigInt, Ix)).GetXml();
        }
        return pictureUrl;
    }

And I get this error in my browser

Failed to load resource: the http://localhost:59913/edit.aspx/LoadLanguages server responded with a status of 404 (Not Found),Failed to load resource: the http://localhost:59913/edit.aspx/LoadLanguagesSelect server responded with a status of 404 (Not Found),Failed to load resource: the http://localhost:59913/edit.aspx/LoadVideoGallery server responded with a status of 404 (Not Found),Failed to load resource: the http://localhost:59913/Edit.aspx/LoadImageGallery server responded with a status of 404 (Not Found)

1

1 Answers

0
votes

I see two issues

First I noticed that your WebMethods do not accept parameters but you are specifting data: "{}" in your ajax call. Try omitting this line when parameters are not necessary. You could return a 404 if the page is looking for a method which accepts an object but doesn't find one. See this answer:

How to call c# method with no parameters and access returned data?

Have you tried using fiddler to watch your AJAX request being made? If so ensure that it is actually going out as a POST request. Otherwise the sever will be searching for a GET method and will not find one, thus returning a 404.

I always use $.ajax({type: "POST""}); as opposed to $.ajax({method: "POST""})