0

Good day to all. I have the flowing problem.

I have 2 domains. On one domain I send an ajax post to the 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.

zozo
  • 8,230
  • 19
  • 79
  • 134
  • Where is http_request.responseText? –  Jun 17 '11 at 12:31
  • This sounds like you're running into an XSS restriction. Additionally, and forgive me as I may have run right past it, you are aware that you are running this in async mode correct? – Dave G Jun 17 '11 at 12:33
  • Consider doing yourself a favor, and use a library (e.g. jQuery). This whole code would be summarized to `$.post(url, parameters, function(){ /* on success here */ });` – mkilmanas Jun 17 '11 at 12:40
  • The httpd_request.responseText is in the handle function. I didn't add it there because is simply etc.rdyState == 4 { alert(http_request.responseText)}. – zozo Jun 17 '11 at 12:44

2 Answers2

4

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

Community
  • 1
  • 1
Sascha Galley
  • 15,711
  • 5
  • 37
  • 51
  • That was my hunch... any fast fixes? (I can't us the proxy and flash sugestions... im looking at json now but if any1 implemented it would be nice) – zozo Jun 17 '11 at 12:49
  • Not so easy... I also need to insert something into the database. I send POST, insert, get the content of the page after insert. – zozo Jun 17 '11 at 13:04
  • 1
    Your Wrong Same Origin Policy has a work around, that is native to it you do not require an outside source http://www.codingforums.com/showthread.php?t=214641 – Barkermn01 Jun 17 '11 at 13:36
  • Ty Barkermn01. I think that will solve my problem. I'll try it tomorrow. +1 – zozo Jun 18 '11 at 15:00
0

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.