Re: [PHP] php/ajax question



On Saturday 30 December 2006 18:56, tedd wrote:
Why can't the php script redirect the browser when called via ajax ?

The browser will not be expecting a page back, and will ignore headers. The
response must be handled by a function you define.

For the sake of a quick demo, if your php accepts an action via GET and
simply prints the location for the redirect, you can do it at the client
end.

// example code

function createRequest() {
try {
request = new XMLHttpRequest();
} catch (trymicrosoft) {
try {
request = new ActiveXObject("Msxml2.XMLHTTP");
} catch (othermicrosoft) {
try {
request = new ActiveXObject("Microsoft.XMLHTTP");
} catch (failed) {
request = null;
}
}
}
if (request == null) alert("Error creating request object!");
}

function sndReq(action) {
createRequest();
var url = "a.php?action=" + action + "&dummy=" + new Date().getTime();
// random stuff at the end is hack to get round browser response cacheing
request.open("GET",url);
request.onreadystatechange = updateReq;
request.send(null);
}

function updateReq() {
if (request.readyState == 4) {
var newLocation = request.responseText;
window.location = newLocation;
}
}
.



Relevant Pages

  • Re: [PHP] php/ajax question
    ... The browser will not be expecting a page back, ... request = new XMLHttpRequest; ... function updateReq() { ... var newLocation = request.responseText; ...
    (php.general)
  • Re: Getting Fox News to work with Firefox
    ... CNN or Fox. ... from them if their Website wouldn't support my choice of Web browser. ... Their response: An on-line application form by e-mail pointing to their ...
    (Fedora)
  • Re: html servlet flow
    ... A browsers' request is always an absolute path, ... Imagine for example, that the browser requests for the url '/random', and the webserver recognizes this as a special address which doesn't actually point to a file, but instead indicates that the server should generate a random 32-bit integer and send its decimal string representation as a response. ...
    (comp.lang.java.programmer)
  • Re: Getting Fox News to work with Firefox
    ... from them if their Website wouldn't support my choice of Web browser. ... Their response: An on-line application form by e-mail pointing to their ... Citibank is not the only credit card company in town, ... I'll use Konqueror as well when it suits what I ...
    (Fedora)
  • Re: Getting Fox News to work with Firefox
    ... from them if their Website wouldn't support my choice of Web browser. ... Their response: An on-line application form by e-mail pointing to their ... Citibank is not the only credit card company in town, ...
    (Fedora)