Javascript Ajax Call总是返回readyState = 1和status = 0

时间:2014-07-19 05:45:48

标签: javascript php jquery html ajax

我通过AJAX发送邮件请求如下。

我总是得到xmlhttp.readyState = 1xmlhttp.status= 0 xmlhttp.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 PART

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

2 个答案:

答案 0 :(得分: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');

希望这有帮助,

谢谢

答案 1 :(得分:0)

你需要添加一个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>