2013-08-25 50 views
-1

夥計們,我感到困惑。我只使用小的JavaScript代碼從URL獲取一些json數據。我也使用螢火蟲。每當時間爲e.readyState == 1 e.status == 0。我找不到任何有用的答案。我在這篇文章中記下了我的腳本代碼。我希望我的朋友和專家程序員能夠儘快幫助我。readyState 4 && status 200總是得到1和0 - (JavaScript)

我的JavaScript功能代碼 -

kwXMLHttpRequestGET:function(a,b,c,i,h,g){ 
    var e=new XMLHttpRequest(); 
    var d=null; 
    if(typeof(h)=="function"){ 

     e.onreadystatechange=function(){ 
      if(e.readyState==4 && e.status==200){ 
       if(typeof(h)=="function"){ 
         if(typeof(e.responseText)==="string"){ 
          d = e.responseText; 
         }else{ 
          if(typeof(e.responseXML)==="object"){ 
           d = e.responseXML; 
          } 
         } 
        h.call(i,d,g); 
       } 
       delete e.onreadystatechange; 
       e.onreadystatechange=null; 
      } 
     }; 
    } 
    e.open(a,b,c); 
    e.send(null); 
}, 
  1. 我傳遞變量側的功能。
  2. 被 「GET」
  3. b爲 「URL」 這是http://localhost:81/stb/database/new_auth/data.json
  4. c爲 「真」
  5. IH和G將在函數中定義

我的問題是,爲什麼它得到1和0每次。

+1

看起來你失敗了同樣的起源測試。您正在傳遞一個端口號,這是一個保證失敗。不知道_localhost_,無論。 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Same_origin_policy_for_JavaScript –

+0

@PaulS。這是我的本地主機Wampserver。它工作正常。 – yeshansachithak

+0

你好像誤解了我;我不確定_localhost_如何與來自網絡瀏覽器的相同來源策略和其他安全措施交互。您不希望您運行的任何本地_.html_文件能夠完全訪問您的計算機,是嗎? –

回答

0

readyState的4個& &狀態200總是得到1和0

爲什麼狀態總是得到0ερ

這很簡單,當你要訪問另一個CORS。您需要將兩行添加到您的服務器應用程序中。要知道那麼好Click Here

最後,你需要兩行接受CORS

header('Content-type: text/html'); 
header('Access-Control-Allow-Origin: *'); 

我希望這將有助於某人。 感謝大家。

相關問題