【问题标题】:Javascript Ajax Call always returns readyState=1 and status=0Javascript Ajax 调用总是返回 readyState=1 和 status=0
【发布时间】:2014-09-10 07:26:25
【问题描述】:

我正在通过 AJAX 发送 post 请求,如下所示。

我总是收到 xmlhttp.readyState = 1xmlhttp.status= 0xmlhttp.responseText 总是

你能告诉我可能是什么问题吗?

我希望 xmlhttp.readyState==4 && xmlhttp.status==200

<script>
//Ajax to send request..
function sendPayment()
{
    if (window.XMLHttpRequest)
      {// code for IE7+, Firefox, Chrome, Opera, Safari
      xmlhttp=new XMLHttpRequest();
      }
    else
      {// code for IE6, IE5
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
        xmlhttp.onreadystatechange=function()
          {
alert(xmlhttp.readyState);// this always returns = 1
alert(xmlhttp.responseText) ; //this is always empty.
          if (xmlhttp.readyState==4 && xmlhttp.status==200)
            {
                if (xmlhttp.responseText=='1')
                {
                    alert('success');
                }   
            }
          }
    xmlhttp.open("POST","payments/callSSL.php",true);
    xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
    xmlhttp.send(Id=100);
    return false;
}
</script>

HTML 部分

<input name="button" type="submit" id="button" value="Confirm" onclick="sendPayment()" />

【问题讨论】:

  • success 警报永远不会显示?
  • xmlhttp.readyState 总是返回 true,而 ajax 总是返回一个值。请指定您的ajax结果状态
  • @adeneo:是的。尽管我在/callSSL.php 文件中发送了一些输出,但我得到的只是空的
  • 检查你的 callSSL.php ,你必须回显不返回你的结果
  • 你不能从另一个域打电话检查这个stackoverflow.com/questions/23053349/…

标签: javascript php jquery html ajax


【解决方案1】:

如果您要调用自己的另一个站点,则必须在您的另一个站点(即(http://my-other-site.com/payments/callSSL.php)中授予访问权限。

将此标题放入您的http://my-other-site.com/payments/callSSL.php

header('Access-Control-Allow-Origin: *'); 

特定页面

header('Access-Control-Allow-Origin: http://www.yourxmlrequestpage.php');

希望这会有所帮助,

谢谢

【讨论】:

  • 好的,如果您担心,请将上面的 * 替换为您发送 xmlhttp 请求的页面 url
  • 上述编辑将避免安全漏洞。让我知道你的状态
【解决方案2】:

你需要添加一个var作为xmlhttp;在开始获取状态结果时请使用下面的代码我修改它,

<script>
//Ajax to send request..
function sendPayment()
{
var xmlhttp;
    if (window.XMLHttpRequest)
      {// code for IE7+, Firefox, Chrome, Opera, Safari
      xmlhttp=new XMLHttpRequest();
      }
    else
      {// code for IE6, IE5
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
        xmlhttp.onreadystatechange=function()
          {
alert(xmlhttp.readyState);// this always returns = 1
alert(xmlhttp.responseText) ; //this is always empty.
          if (xmlhttp.readyState==4 && xmlhttp.status==200)
            {
                if (xmlhttp.responseText=='1')
                {
                    alert('success');
                }   
            }
          }
    xmlhttp.open("POST","payments/callSSL.php",true);
    xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
    xmlhttp.send(Id=100);
    return false;
}
</script>

【讨论】:

  • 相同的结果.. 没用.. 我使用了很多 ajax 请求,但我没有将其全部指定为 var
  • xmlHttpRequest.onreadystatechange = processRequest; 之后添加xmlHttpRequest.send();。请试试这个可能对你有帮助。
  • 你请使用'var xmlhttp;'希望这有助于或确保它是 processRequest 函数。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-10-24
  • 2018-12-24
  • 2014-12-17
  • 2011-10-24
  • 1970-01-01
  • 2013-02-26
  • 2014-09-21
相关资源
最近更新 更多