JQuery Ajax call gives 404 'Resource Not Found' Error but normal URL call is fine

前端 未结 7 2296
别跟我提以往
别跟我提以往 2020-12-31 18:29

I have a weird problem when using JQuery call in my ASP.NET MVC project. I found that the Ajax call gives 404 ( resource not found error). But when I use the usual URL GET c

相关标签:
7条回答
  • 2020-12-31 18:36

    Instead of hard-coding the URL, you might want to try a UrlHelper:

    $(function() {
        $("#username").click(function() {
            var url = '<%= UrlHelper.Action("GetSoftwareChoice", "ViewRecord") %>';
            $.getJSON(url, {username: '123'}, function(data) {
                alert(data);
            });
        });
    });
    
    0 讨论(0)
  • 2020-12-31 18:45

    Replace the equal sign with a colon:

    $(function() {
    $("#username").click(function() {
            $.getJSON("ViewRecord/GetSoftwareChoice", {username:'123'},
        function(data) {
            alert(data);
        });
        });
    });
    
    0 讨论(0)
  • 2020-12-31 18:49

    Use Firefox Firebug add on, and watch what request gets made by Jquery...

    Is it possible that the page that this Jquery runs in is in a subdirectory, in which case the request will not be relative root as the http://myapp/ "typed in" url is?

    Also, I am guessing the code you specified above is not actually the code you are using (which is completely reasonable, I rairly post code as-is). Because

    $.getJSON("ViewRecord/GetSoftwareChoice", {username='123'},
    

    the = sign between username and '123' is invalid JS as far as I know. So I'm betting there is some goofy detail in the the real code that is causing the problem.

    0 讨论(0)
  • 2020-12-31 18:51

    Old function :

    function Chart() {
    
        var url = "../Home/Pie/";
        $.ajax({
            url: url,
            data: {},
            cache: false,
            type: "POST",
            success: function (data) {
                var chartData = data;
                createChart(chartData);
                $(document).bind("kendo:skinChange", createChart);
            },
            error: function (xhr, status, error) {
                $('#alertdialog').html(status);
                $('#alertdialog').dialog('open');
                return false;
            }
        });
    }
    

    Answers : var url = "Home/Pie/"; Removed ../ from url

    0 讨论(0)
  • 2020-12-31 18:52
    $(function() {
        $("#username").click(function() {
            $.getJSON('<%= Url.Action("GetSoftwareChoice", "ViewRecord")%>',{username: '123'}, function(data) {
                alert(data);
            });
        });
    });
    
    0 讨论(0)
  • 2020-12-31 19:00

    I fix this problem by using FireBug to show me the request that was generated by JQuery. To my amazement, the url generated is

    http://localhost/ViewRecord/ViewRecord/GetSoftwareChoice?username=123
    

    for the JSON call:

    $(function() {
    $("#username").click(function() {
            $.getJSON("ViewRecord/GetSoftwareChoice", {username:'123'},
        function(data) {
            alert(data);
        });
        });
    });
    

    So I just have to change the $.getJSON line to

    $.getJSON("GetSoftwareChoice", {username:'123'},
    

    Alternatively, use the forward slash:

     $.getJSON("/ViewRecord/GetSoftwareChoice", {username:'123'},
    
    0 讨论(0)
提交回复
热议问题