jQuery Ajax post - 404 error

2019-06-24 03:47发布

问题:

I'm posting to an ActionMethod to retrieve some customer counts. The post works fine from my local machine. When deployed to another server its throwing 404 errors. My post is below. I'm not sure why this has stopped working.

var urlStr = "/Items/ItemCount/";                    
                jQuery.ajax({
                    type: 'POST',
                    dataType: 'json',
                    url: urlStr,
                    async: false, //wait on the result to be  returned...                    
                    success: function (DocData) {
                        window.currentCount = DocData[0];
                        window.maximumAllowed = DocData[1];
                    },
                    cache: false,
                    error: function (request, status, error) {
                        alert(request.responseText);
                    }
                });

<HttpPost()> _
    Function ItemCount() As JsonResult

        Dim Items As List(Of MyItems) = GetItems()
        Dim Total As Integer = Items .Count            
        Dim Max = 5
        Dim Data As New ArrayList
        Data.Add(TotalDocs)
        Data.Add(MaxDocs)
        Return Json(Data)
    End Function

回答1:

Probably on the other machine you have deployed the application in a virtual directory and so the absolute url you're using could not get found.

Try to use this code instead

var urlStr = '<%: Url.Content( "~/Items/ItemCount" ) %>';


回答2:

Your urlStr is set to /Items/ItemCount/. When pushed to a server it's going to try to look up those files from the root (because of the first '/'). You should try to use an absolute path to fix this.