AJAX Ready State 4, but empty text
I call a page using AJAX, requesting a search for something. But the response text is always empty, although the ready state is 4.
function process() {
var requestPage = "http://some.page/search.php";
var xmlHTTP;
var searchString = "test";
if (window.XMLHttpRequest) {
xmlHTTP = new XMLHttpRequest开发者_如何学Python();
} else {
xmlHTTP = new ActiveXObject("Microsoft.XMLHTTP");
}
var results;
xmlHTTP.open("GET", requestPage + "?search=" + searchString, true);
xmlHTTP.send();
xmlHTTP.onreadystatechange = function() {
results = xmlHTTP.responseText;
alert("r: " + results + " rs: " + xmlHTTP.readyState + " st: " + xmlHTTP.status);
}
}
The results I get from this are
r: rs: 2 st: 0
r: rs: 4 st: 0
So, the request is done successfully, but the HTTP status isn't 200. But: I tracked the request using WireShark and the result package of the request shows as status 200 and even contains the complete result of the request, see below:
7014 190.663287 some.page local.pc HTTP HTTP/1.1 200 OK (text/html)
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Set-Cookie: JSESSIONID=xxx; Path=/xxx
Content-Type: text/html;charset=iso-8859-1
Content-Length: 3108
Date: Tue, 19 Apr 2011 13:05:41 GMT
<html>
<head>
<title>Search</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body bgcolor="white">
...
So; why don't I get the status 200 on my AJAX request when it obviously succeded?
So your problem is that you are requesting a page with a different port.
The same origin policy says that the protocol, port and host should be the same.
See
http://en.wikipedia.org/wiki/Same_origin_policy and https://developer.mozilla.org/en/Same_origin_policy_for_JavaScript
精彩评论