request.send()后重定向-请求未被提交,页面重定向

Redirect after request.send() - request is not posted, page redirects

本文关键字:重定向 提交 send request 请求      更新时间:2023-09-26

我有一个基本的ajax评论表单,其中包含一个文本区和是/否单选对象。

  • 如果选择"是",则发布用户的评论,然后他们被带到新的一页。
  • 如果"否",评论将被发布并保持在同一页面上。

实际情况是,当用户选择"是"时,评论不会发布,而且页面还是会重定向。

下面是post脚本:

    //
    flag_yesno      = getRadioValue('form_comments', 'flag_yesno');
    request.open('POST', 'process.php?t=' + new Date().getTime());
    request.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
    request.onreadystatechange = function()
    {
        if(request.readyState == 4)
        {
            // put response text in the comment_posts div
            comment_posts.innerHTML     = request.responseText;
        }
    }
    // set up POST parameters
    var params = "&foo=" + foo + "&bar=" + bar;
    // send it on over!
    request.send(params);
    // redirect if needed
    if (flag_yesno=='yes')
    {
        window.location = '/foo.php?foo='+foo;
    }

是否有一种方法来处理重定向后的request.send()被解雇?重定向似乎破坏了这部分

等待呼叫完成。等待"直到发射"是很难衡量的。

if(request.readyState == 4)
{
  if (flag_yesno == 'yes')
  {
    // redirect if needed
    window.location = '/foo.php?foo='+foo;
  } 
  else
  {
    // put response text in the comment_posts div
    comment_posts.innerHTML     = request.responseText;
  } 
}