English 中文(简体)
没有收到 XMLHttp 请求回复
原标题:Not getting the response from XMLHttpRequest

我想在Twitter.com网站上的推特上搜索“板球”一词。 问题在于我是否使用电话在手机中运行这个代码,

function appReady(){
    var ajax = new XMLHttpRequest();
    ajax.open("GET","http://search.twitter.com/search.json?q=Hemant",true);
    ajax.send();

    ajax.onreadystatechange=function(){
       if(ajax.readyState==4 && (ajax.status==200||ajax.status==0)){
         var data = JSON.parse(ajax.responseText);
         var theResults = data.results;
         var theHTML =   ;

         for(var i=0;i<theResults.length;i++){
           alert(theResults[i].text);
         }
         document.getElementById( main ).innerHTML = theHTML;
       }
    }
}

显示错误的警示 : < pengenemcode> JSON.parse: 数据意外结束

告诉我哪里出错了?

最佳回答

你试过用Jquery和GetJson的功能做吗?

function appReady(){
  $.getJSON( http://search.twitter.com/search.json?q=Hemant&callback=? , function(json) {
      var theHtml = "";
      var theResults = json.results;
      for(var i=0;i<theResults.length;i++){
           theHtml += theResults[i].text;
         }
      $("#main").html(theHtml);
  });
}​

示例 < a href=>""http://jsfiddle.net/eBUcv/2/" rel="nofollow" >Link 。 (将 apReady () 更改为文件.ready ())

问题回答

首先,这里有一个可行的答案,没有jQuery。

    <html>
    <head>
    <script type="text/javascript">
        function callTwitterSearch() {
          var url = "http://search.twitter.com/search.json?q=Hemant&callback=parseRequest";
          //create and inject the script element
          var script = document.createElement( script );
          script.setAttribute( src , url);
          document.getElementsByTagName( head )[0].appendChild(script);
      }

      function parseRequest(response) {
//This response object is the actual JSON parsed response from twitters api.
          alert(response.results.length);//Simply print the result list s size
      }
    </script>
    </head>
      <body onLoad="callTwitterSearch()">
      <body>
    </html>

第二,

在 jQuery 中, 如果您指定回调=?, 您实际上正在将调用从 < 强势 > json < /强势 > 改为 < 强势 > jsonp < /强势 > 。 jsonp 被很好地解释为 < a href=" "http:// en.wikipedia. org/ wiki/JSONP" rel= "no follow" 。

json calls have to be made from the same domain. jsonp calls are for cross domain calls. since your local machine (and I m assuming here :)) is not on the twitter domain, your call fails.

<div类="tweet" 似乎缺少一个封闭括号, 它可能会混淆您 HTML 字符串的其余部分, 导致其解析方式与您喜欢的不一样 。

这里您错误使用 eval 。 您想要做的是 va data = eval( ajax. referenceText); . however , 这真是个坏主意, 因为网络攻击者可以用 window. close () 或甚或一些东西取代您的 JSON 回应。 您应该做的是 va data = JSON.parse (ax. referText); 并使用 < a href=" https://gthub.com/douglascrockford/ Json-js" rel=" nofol" JSON 图书馆 用于浏览器, 而不支持 JSON < /code> 对象。





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.