首页 > 解决方案 > JS/AJAX 加载资源失败,服务器响应状态为 404(未找到)

问题描述

在这里,当用户单击网站任何页面上的 lnkfavourite 按钮时,我试图让用户选择主页。当我将其他页面设置为主页时,它可以按预期工作,但不能使用 default.aspx。此页面位于根目录。
VB页面后面:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
 lnkFavorite.Attributes("onclick") = "clearFave();"
end sub

JS/AJAX:

 function clearFave() {
    if (window.XMLHttpRequest) {
    req = new XMLHttpRequest();
    var params = "u=default.aspx";
    req.open("POST", "/myaccount/favorite.aspx", true);
    req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    //req.setRequestHeader("Content-length", params.length);
    //req.setRequestHeader("Connection", "close");
    req.send(params);     //Error here

    var img = document.getElementById("ctl00_pageContent_imgFavorite");
    if (img) {
        img.src = img.src.replace("/favorite.png", "/unfavorite.png");
    }
    alert("This page is now set as your BERT home.");
  }
}

最喜欢的页面后面:

Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
    Try
        Dim u As String = "default.aspx"
        If Request("u") IsNot Nothing Then
            u = Request("u").Replace("|", "&")
        End If
        Profile.SetPropertyValue("favorite", u)
        Profile.Save()
    Catch ex As Exception

End Sub

标签: javascriptajaxvb.net

解决方案


尝试更改帖子 URL,使 URL 特定。像这样http://doman:port/a-page

req.open('POST', 'http://yourdomain:port//myaccount/favorite.aspx')

推荐阅读