开发者

readyState 4 - redirect url is that possible?

I was wondering if it is possible when u have readyState == 4, to redirect to a page..

    //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

so on readyState 4 , i want to redirect to a page, after the form is sended i want to redirect to a page. on php mail function, <?php header(location: "url"); ?> is not working开发者_StackOverflow中文版 so i really need to have it in ajax/javascript...

currently it just not working. any suggestion?


do u mean you want to redirect once the ajax succeed? If yes, try using jquery ajax() function and add the redirect code as the success callback

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

post() is an easier alternative http://docs.jquery.com/Post


Is your code inside the sendRequest.onreadystatechange callback? Even if you don't anticipate a 200 return it should be within that callback.

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.

[EDIT] Have you checked the error console? Also, does this page involve frames by chance?

[EDIT 2]

I've built a jsbin for this and it does work on Firefox and 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. 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

ERRORS: Refused to set unsafe header "Connection" XMLHttpRequest cannot load http://jromero.me/. Origin http://jsbin.com is not allowed by Access-Control-Allow-Origin. jromero.meFailed to load resource

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?


Thank you 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.

This lead to only 1 fault, and that is in the form. had onSubmit="return checkForm(this);" action="" It should be onSubmit="checkForm(this); return false;" action=""

with the onSubmit event return a value of false, so the form doesn't submit as usual. and constructing the js and ajax properly to finalize the send form. validate the form -> when ok -> prepare the form -> call ajax -> all valid -> send form -> redirect to thank you page.

Thank all for the help!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜