开发者

javascript, php, ajax - AJAX response is always empty

Good day to all. I have the flowing problem.

I have 2 domains. On one domain I send an ajax post to t开发者_开发知识库he other and expect some results. The problem is that the response is always empty. If I inspect the net tab the request looks alright (the post data is fine), it doesn't receive any error, it ends (I put an alert on the handle response function to check what the response is). I tried sending a request to random domains (like example.com) to see if I get anything. The response is the same... none.

Here is the script I use:

    function sendReqPost(url) {
      http_request = false;
      if (window.XMLHttpRequest) { // Mozilla, Safari,...
         http_request = new XMLHttpRequest();
         if (http_request.overrideMimeType) {
            // set type accordingly to anticipated content type
            //http_request.overrideMimeType('text/xml');
            http_request.overrideMimeType('text/html');
         }
      } else if (window.ActiveXObject) { // IE
         try {
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
         } catch (e) {
            try {
               http_request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
         }
      }
      if (!http_request) {
         alert('Cannot create XMLHTTP instance');
         return false;
      }
      //http_request.onreadystatechange = handleResponseAccept;
      http_request.open('POST', url, true);
      http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      http_request.setRequestHeader("Content-length", parameters.length);
      http_request.setRequestHeader("Connection", "close"); 
      //parameters is a global variable with the post data.     
      http_request.send(parameters);
    }

I double checked everything in the script... I also inserted echos in the requested php page to see if I get anything. Whatever I do the response is empty. P.S. On another domain the ajax script worked fine. Exactly the same.


I have 2 domains. On one domain I send an ajax post to the other and expect some results.

There's your problem. This is because of the Same Origin Policy in JavaScript. And thats why...

...on another domain the ajax script worked fine.

There are some workarounds though, called Cross Domain Ajax.

For your needs, since you apparently want HTML and not JSON, I would suggest a small PHP script to get the content from the other domain and forward it to your client side. This would be called Ajax proxy.

See this question


I don't see your http_request.responseText, it returns what is echo'ed in the request URL.

So try add this:

http_request.onreadystatechange = function () {
    if (http_request.readyState == 4) {
        if (http_request.status == 200) {
            alert(http_request.responseText);
        } else {
            alert("An error occurred: "+ http_request.statusText);
        }
     }
};

Before:

//parameters is a global variable with the post data. 

See if it works.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜