jQuery Ajax post - 404 error

杀马特。学长 韩版系。学妹 提交于 2019-12-10 15:27:45

问题


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.



来源:https://stackoverflow.com/questions/4378958/jquery-ajax-post-404-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!