接收AJAX HTTP响应code为0 [英] Receiving AJAX HTTP Response Code as 0

查看:1431
本文介绍了接收AJAX HTTP响应code为0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个pretty的简单的AJAX和PHP code。在呼吁通过AJAX它收到响应code为0的PHP code为成功运行PHP的,但我不能得到响应。这是什么状态0表示,我怎么能解决这个问题?

 函数confirmUser(ID)
{
    XMLHTTP = GetXmlHttpObject();
    REGID = ID;
    如果(XMLHTTP == NULL){
        警报(浏览器不支持HTTP请求);
        返回;
    }
    VAR URL =confirm.php;
    URL =网址+ + IDID =?;
    URL =网址+&放大器; A =确认;
    xmlhttp.onreadystatechange =功能(){
        如果(xmlhttp.readyState == 4和&安培; xmlhttp.status == 200){
            $(#txtHint+ REGID)的.text(等待确认);
        } 其他 {
            警报(xmlhttp.status); //这个显示0
        }
    };
    xmlhttp.open(GET,URL,真正的);
    xmlhttp.send(空);
}
 

好了,这是我使用的JavaScript。请原谅我,如果我已经添加什么比这一点。还告诉我,我错过了。 我AP preciate你的帮助

GetXmlHttpObject功能:

 函数GetXmlHttpObject()
{
    如果(window.XMLHtt prequest){
        // $ C $下IE7 +,火狐,Chrome,歌剧,Safari浏览器
        返回新XMLHtt prequest();
    }
    如果(window.ActiveXObject){
        // code对IE6,IE5
        返回新的ActiveXObject(Microsoft.XMLHTTP);
    }
    返回null;
}
 

解决方案

我知道人们可能不爱听,但是这正是JS框架是。为什么乱用所有的各种浏览器的倾向和灾害进行了自定义AJAX时,你可以通过做jQuery的一个简单的AJAX调用呼叫。

基本上,你是重新发明轮子,并且没有任何理由。有你的PHP返回JSON数据,并与成功code嵌入一个变量,如果你需要测试这一点。

 <脚本SRC =jquery.js和>< / SCRIPT>
<脚本>
  $获得(myphp.php,{ID:是的,胡说:东西},功能(数据){
  如果(data.success == 1){
    警报(得到的数据);
  } 其他 {
    警报(没有得到数据);
  }
},JSON);
< / SCRIPT>
 

繁荣,你现在有跨浏览器的AJAX。

I have a pretty simple AJAX and PHP code. While calling the PHP through the AJAX it receives the response code as 0. The PHP code is successfully run, but I can't get the response. What does this status '0' denote and how can I solve this?

function confirmUser(id)
{
    xmlhttp=GetXmlHttpObject();
    regid = id;
    if (xmlhttp==null)  {
        alert ("Browser does not support HTTP Request");
        return;
    }
    var url="confirm.php";
    url=url+"?id="+id;
    url=url+"&a=confirm";
    xmlhttp.onreadystatechange=function() {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            $("#txtHint" + regid).text("Awaiting confirmation");
        } else {
            alert (xmlhttp.status); //this shows '0'
        }
    };
    xmlhttp.open("GET",url,true);
    xmlhttp.send(null);
}

Well, this is the javascript I used. Pardon me if I should've added anything more than this. Also tell me what I missed. I appreciate your help

GetXmlHttpObject function:

function GetXmlHttpObject()
{
    if (window.XMLHttpRequest) {
        // code for IE7+, Firefox, Chrome, Opera, Safari
        return new XMLHttpRequest();
    }
    if (window.ActiveXObject) {
        // code for IE6, IE5
        return new ActiveXObject("Microsoft.XMLHTTP");
    }
    return null;
}

解决方案

I know people may not want to hear it, but this is exactly what JS frameworks are for. Why mess with all of the various browser inclinations and disasters that are custom AJAX calls when you can just do a simple AJAX call through jQuery.

Basically, you are reinventing the wheel, and for no reason. Have your php return JSON data, and embed a variable in with the success code if you need to test for that.

<script src="jquery.js"></script>
<script>
  $.get("myphp.php", { id : "yes", blah : "stuff" }, function(data) {
  if (data.success == 1) {
    alert("got data");
  } else {
    alert("didn't get data");
  }
},"json");
</script>

Boom, you now have cross-browser AJAX.

这篇关于接收AJAX HTTP响应code为0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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