简体   繁体   English

javascript,php,ajax-AJAX响应始终为空

[英]javascript, php, ajax - AJAX response is always empty

Good day to all. 祝大家有美好的一天。 I have the flowing problem. 我有流动的问题。

I have 2 domains. 我有2个域。 On one domain I send an ajax post to the other and expect some results. 在一个域上,我向另一个域发送ajax帖子,并期待一些结果。 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). 如果我检查了net选项卡,请求看起来就没问题(发布数据很好),它没有收到任何错误,它结束了(我在handle响应函数上发出了警报,以检查响应是什么)。 I tried sending a request to random domains (like example.com) to see if I get anything. 我尝试将请求发送到随机域(例如example.com),以查看是否收到任何内容。 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. 我仔细检查了脚本中的所有内容...我还在请求的php页面中插入了回声,以查看是否得到了任何东西。 Whatever I do the response is empty. 无论我做什么,反应都是空的。 PS On another domain the ajax script worked fine. PS在另一个域上,ajax脚本运行良好。 Exactly the same. 一模一样。

I have 2 domains. 我有2个域。 On one domain I send an ajax post to the other and expect some results. 在一个域上,我向另一个域发送ajax帖子,并期待一些结果。

There's your problem. 有你的问题。 This is because of the Same Origin Policy in JavaScript. 这是由于JavaScript中的同源策略 And thats why... 那就是为什么...

...on another domain the ajax script worked fine. ...在另一个域上,ajax脚本运行良好。

There are some workarounds though, called Cross Domain Ajax . 但是,有一些变通方法称为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. 为了满足您的需求,由于您显然需要HTML而不是JSON,我建议使用一个小的PHP脚本从其他域获取内容并将其转发给客户端。 This would be called Ajax proxy . 这将称为Ajax代理

See this question 看到这个问题

I don't see your http_request.responseText , it returns what is echo 'ed in the request URL. 我看不到您的http_request.responseText ,它返回了请求URL中echo的内容。

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. 看看是否可行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM