【问题标题】:javascript ajax not working properlyjavascript ajax 无法正常工作
【发布时间】:2014-10-06 10:11:49
【问题描述】:

我有以下 javascript 代码

        alert("");

    var xmlhttp;
    if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    }
    else {// code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange = function () {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            //document.getElementById("myDiv").innerHTML = xmlhttp.responseText;
        }
    }

    xmlhttp.open("GET", "lp.aspx", true);
    xmlhttp.send();

我的服务器代码如下

protected void Page_Load(object sender, EventArgs e)
{
    if (Request.QueryString["pb"] != null) 
    {
        Response.Redirect("main.aspx");
    }
}

问题是当我将查询字符串与我的 javascript ajax 请求连接时,如下所示

xmlhttp.open("GET", "lp.aspx?pb=true", true);

它停止向服务器发送请求。我的意思是当我调试页面时 page_load 事件没有触发。,

这可能是什么原因?

【问题讨论】:

  • 代码看起来没什么问题。浏览器控制台有错误吗?
  • 你用的是什么浏览器?我尝试使用 chrome,在 if(Request.QueryString["pb"] != null) 中设置断点,代码停在那里。

标签: javascript jquery asp.net .net ajax


【解决方案1】:

这是因为 response.redirect 不起作用。

删除它并在 javascript 中使用 window.location

【讨论】:

    猜你喜欢
    • 2023-03-11
    • 1970-01-01
    • 2013-05-21
    • 1970-01-01
    • 1970-01-01
    • 2014-01-11
    • 1970-01-01
    • 2014-07-23
    相关资源
    最近更新 更多