jQuery Ajax post - 404 error

2019-06-24 04:09发布

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

2条回答
做自己的国王
2楼-- · 2019-06-24 04:19

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.

查看更多
手持菜刀,她持情操
3楼-- · 2019-06-24 04:34

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" ) %>';
查看更多
登录 后发表回答