0

我在使用下面的代码时遇到问题,每当我尝试从网站请求 JSON 数据时,我总是会返回响应代码 0。有人知道为什么吗?如果我要访问该网站,我只需输入正确的登录信息即可获取数据。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title></title>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
    <script src="Base64.js" type="text/javascript"></script>
    <script type="text/javascript">

        function alertContents(HTTPobj) {

            if (HTTPobj.readyState == 4) {
                // everything is good, the response is received
                if ((HTTPobj.status == 200) || (HTTPobj.status == 0)) {
                    // FIXME: perhaps a better example is to *replace* some text in the page.
                    var htmlDoc = document.createElement('div'); // Create a new, empty DIV node.
                    htmlDoc.innerHTML = HTTPobj.responseText; // Place the returned HTML page inside the new node.
                    alert("The response was: " + HTTPobj.status + HTTPobj.responseText);
                    //var jsonData = eval('(' + HTTP.responseText + ')');
                    //parseJson(HTTP.responseText);

                }
                else {
                    alert('There was a problem with the request. ' + HTTPobj.status + HTTPobj.responseText);
                }
            }
        }


        }

        function makeBaseAuth(user,password) {
            var tok = user + ':' + password;
            var hash = Base64.encode(tok);
            alert(hash);
            return "Basic " + hash;
        }

        function getInput() {

            var getUser = document.input.user.value;
            var getPass = document.input.pass.value;
            var logData = makeBaseAuth(getUser, getPass);
            var url = 'http://www.tt-galaxi.com/thubrest/login';
            //  var url = 'http://www.tt-galaxi.com/thubrest/users/%@/trips',username;
            //    var url = 'http://www.tt-galaxi.com/thubrest/parkingbays';
            var HTTPobj = new XMLHttpRequest();
            HTTPobj.onreadystatechange = function () { alertContents(HTTPobj); };
            var test = HTTPobj.open('GET', url);
            HTTPobj.setRequestHeader('Authorization', logData);
            HTTPobj.send();

        }



    </script>
    </head>
    <body>

    <form name="input" action="">
    Username: <input type="text" name="user" />
    <br />
    Password:<input type="text" name="pass" />    
    <br />
    <input id="Button1" type="button" value="Submit"  onclick="getInput()"/>
    </form>
    </body>
    </html>
4

2 回答 2

0

看起来您正在尝试从与您的来源不同的主机请求数据。为了详细说明这一点,如果您有一个名为 www.example.com 的站点在主机 A 中运行,并且您尝试向主机 B 中运行的 wwww.somethingelse.com 发出请求,您将无法从主机 B 获取数据,因为大多数现代浏览器都执行相同的来源策略,禁止以跨站点方式使用 AJAX 请求。如果浏览器检测到这种“非法请求”,它会阻止来自不同主机的传入数据。

但是,如果您拥有该其他主机,则可以显式将标头传递给浏览器,该标头基本上告诉浏览器允许来自该其他主机的跨站点数据。在此处阅读有关此内容的更多信息。

于 2012-06-06T07:48:58.157 回答
0

status代码将0用于非法跨域请求,请参阅XMLHttpRequest 状态 0(responseText 为空)XMLHTTPRequest.status 返回 0 并且 responseText 在 FireFox 3.5 中为空

此外,在 FF 上,状态代码将0用于local file://requests

于 2012-06-06T07:52:23.363 回答