javascript,php,ajax - AJAX响应始终为空 [英] javascript, php, ajax - AJAX response is always empty

查看:255
本文介绍了javascript,php,ajax - AJAX响应始终为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对所有人来说都是美好的一天。我有流动的问题。



我有2个域名。在一个域上,我发送一个ajax的帖子到另一个域,并期待一些结果。问题是响应始终为空。如果我检查网络选项卡,请求看起来好(发布数据很好),它没有收到任何错误,它结束(我在句柄响应函数上发出警报来检查响应是什么)。我尝试发送一个请求到随机域(如example.com),看看是否有任何东西。答案是一样的...没有。



这是我使用的脚本:

  function sendReqPost(url ){
http_request = false;
if(window.XMLHttpRequest){// Mozilla,Safari,...
http_request = new XMLHttpRequest();
if(http_request.overrideMimeType){
//根据预期的内容类型设置类型
//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('无法创建XMLHTTP实例');
返回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);
//参数是具有后置数据的全局变量。
http_request.send(参数);
}

我重新检查了脚本中的所有内容...我还将echos插入请求php页面查看是否有任何东西。无论我做什么回应是空的。
P.S.在另一个域上,ajax脚本工作正常。完全一样。

解决方案


我有2个域名。在一个域上,我发送一个ajax的帖子给另一个,并期待一些结果。


有你的问题。这是因为JavaScript中的相同来源策略。这就是为什么...


...在另一个域上,ajax脚本工作正常。


< blockquote>

虽然有一些解决方法,称为跨域Ajax



对于您的需求,由于您显然希望HTML而不是JSON,因此我建议使用一个小型PHP脚本来从其他域获取内容并转发到您的客户端。这将被称为 Ajax代理



请参阅这个问题


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.

解决方案

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

这篇关于javascript,php,ajax - AJAX响应始终为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆