简体   繁体   English

readyState 4-可以重定向网址吗?

[英]readyState 4 - redirect url is that possible?

I was wondering if it is possible when u have readyState == 4, to redirect to a page.. 我想知道当u具有readyState == 4时是否有可能重定向到页面。

    //load the send form
    if (sendRequest) {
        sendRequest.open("POST", urlRequest, true);
        sendRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
        sendRequest.setRequestHeader("Connection", "close");
        sendRequest.onreadystatechange = displayStatus;
        sendRequest.send(sendData);
    } else {
        alert("fails"); //alert if request fails
    }
}

function displayStatus() {
    if (sendRequest.readyState == 4) {
        alert("send");
    // redirect to thank you page? 


    } else {
        alert("send fails"); 

} //end readyState

} }

i am not going to get 200 我不会得到200

so on readyState 4 , i want to redirect to a page, after the form is sended i want to redirect to a page. 所以在readyState 4上,我想重定向到页面,发送表格后,我想重定向到页面。 on php mail function, <?php header(location: "url"); ?> 在php邮件功能上, <?php header(location: "url"); ?> <?php header(location: "url"); ?> is not working so i really need to have it in ajax/javascript... <?php header(location: "url"); ?>无法正常工作,因此我真的需要在Ajax / javascript中使用它...

currently it just not working. 目前,它只是无法正常工作。 any suggestion? 有什么建议吗?

do u mean you want to redirect once the ajax succeed? 您的意思是一旦ajax成功,您是否要重定向? If yes, try using jquery ajax() function and add the redirect code as the success callback 如果是,请尝试使用jquery ajax()函数并将重定向代码添加为成功回调

http://api.jquery.com/jQuery.ajax/ http://api.jquery.com/jQuery.ajax/

post() is an easier alternative http://docs.jquery.com/Post post()是更简单的替代方法http://docs.jquery.com/Post

Is your code inside the sendRequest.onreadystatechange callback? 您的代码是否位于sendRequest.onreadystatechange回调中? Even if you don't anticipate a 200 return it should be within that callback. 即使您不希望返回200,它也应该在该回调中。

Try 尝试

sendRequest.onreadystatechange=function(){
  //DEBUG
  alert("ready state change: "+ sendRequest.readyStat);
  if (sendRequest.readyState == 4) {
    alert("form sended");
    window.location = "http://www.mywebsite.com";
  }
}

And Pointy is right, Server side won't work for two main reasons, one the header has already been sent for the page making the request and because the page requested is AJAX so only the requested page will change not the current one. 而Pointy是正确的,服务器端将不起作用有两个主要原因,其一是已经向发出请求的页面发送了标头,并且因为请求的页面是AJAX,所以仅被请求的页面不会更改当前页面。

[EDIT] Have you checked the error console? [EDIT]您检查了错误控制台吗? Also, does this page involve frames by chance? 另外,此页面是否偶然涉及框架?

[EDIT 2] [编辑2]

I've built a jsbin for this and it does work on Firefox and Chrome... http://jsbin.com/umufi4/2/edit 我为此构建了一个jsbin,它确实可以在Firefox和Chrome上运行... http://jsbin.com/umufi4/2/edit

I'm fairly certain that it's not the location change you have issues with if you use 'http://' infront of it but how your sendRequest is constructed. 我可以肯定地说,如果您在前面使用'http://',这不是您遇到的位置更改,而是sendRequest的构造方式。 Here are some errors I've caught some I'm sure are related to jsbin and remote access but if they exists on your end it might be preventing it from getting to window.location 这是我发现的一些错误,我确信它们与jsbin和远程访问有关,但是如果它们存在于您端,则可能会阻止它进入window.location

ERRORS: Refused to set unsafe header "Connection" XMLHttpRequest cannot load http://jromero.me/ . 错误:拒绝设置不安全的标题“ Connection” XMLHttpRequest无法加载http://jromero.me/ Origin http://jsbin.com is not allowed by Access-Control-Allow-Origin. Access-Control-Allow-Origin不允许使用来源http://jsbin.com jromero.meFailed to load resource jromero.me无法加载资源

I've got a few questions: 我有几个问题:

  1. Is this a cross domain request? 这是跨域请求吗?
  2. Do you get the "send" alert box? 您是否收到“发送”警报框?
  3. Is sendRequest a global variable? sendRequest是全局变量吗?

Thank you J.Romero. 谢谢J.Romero。 With your input i have trace down the problem and construct the script a bit different. 根据您的输入,我已经找到问题所在,并且构建了一些不同的脚本。

It was not a cross domain issue - those settings are set properly. 这不是跨域问题-这些设置设置正确。 Alert box is correct and the sendRequest is global, so all functions can call it. 警报框是正确的,并且sendRequest是全局的,因此所有函数都可以调用它。

This lead to only 1 fault, and that is in the form. 这仅导致1个故障,形式为。 had onSubmit="return checkForm(this);" action="" onSubmit="return checkForm(this);" action="" onSubmit="return checkForm(this);" action="" It should be onSubmit="checkForm(this); return false;" action="" onSubmit="return checkForm(this);" action=""应该为onSubmit="checkForm(this); return false;" action="" onSubmit="checkForm(this); return false;" action=""

with the onSubmit event return a value of false, so the form doesn't submit as usual. 使用onSubmit事件返回的值为false,因此该表单不会像往常一样提交。 and constructing the js and ajax properly to finalize the send form. 并正确构造js和ajax以完成发送表单。 validate the form -> when ok -> prepare the form -> call ajax -> all valid -> send form -> redirect to thank you page. 验证表单->确定->准备表单->调用ajax->所有有效->发送表单->重定向至“谢谢”页面。

Thank all for the help! 谢谢大家的帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM