2014-10-28 90 views
0

我想使用json獲取api值。如果我點擊json按鈕,我沒有得到任何迴應,我不知道爲什麼這是不是昨天運行我已經檢查了api方法,它只是在後。我不知道我缺乏。 這裏是我的代碼:我想使用json.if獲取api值,如果我單擊json按鈕,我沒有得到任何迴應

<script type="text/javascript"> 
function json() 
{ 
    xmlhttp= new XMLHttpRequest(); 
    var url="http://new.ezeeinfosolutions.com/busservices/auth/getAuthToken?namespaceCode=demo&[email protected]&password=newnew&devicemedium=WEB"; 
    alert(url); 
    //var url="dbarr.php"; 
    xmlhttp.onreadystatechange=function() 
    { 
     if(xmlhttp.readyState==4 && xmlhttp.status==200) 
     { 
      var ret_arr=JSON.parse(xmlhttp.responseText); 
      json_arr(ret_arr); 
     } 
    } 
    xmlhttp.open("POST",url,true); 
    xmlhttp.send(); 
} 
function json_arr(x) 
{ 
    var res=""; 
    var i; 
    for(i=0;i<x.length;i++) 
    { 
     res+=x[i].name+" "+x[i].mobile+"</br>"; 
    } 
    document.getElementById('print').innerHTML=res; 
} 
</script> 

<form name="f1" action="" method="post"> 
<input type="submit" onClick="json();" value="Json"> 
<p id="print"></p> 
</form> 

回答

0

他們可以幾個可能的解決辦法,如果其中任何一個讓你成功比chear了:)

  1. 嘗試設置響應類型。

    xmlhttp.responseType ='json';

  2. 嘗試使用xmlhttp.response代替xmlhttp.responseText

  3. 使用這個例子來比較。

    var getJSON = function(url, successHandler, errorHandler) { 
        var xhr = typeof XMLHttpRequest != 'undefined' 
        ? new XMLHttpRequest() 
        : new ActiveXObject('Microsoft.XMLHTTP'); 
        xhr.open('get', url, true); 
        xhr.responseType = 'json'; 
        xhr.onreadystatechange = function() { 
        var status; 
        var data; 
        // http://xhr.spec.whatwg.org/#dom-xmlhttprequest-readystate 
        if (xhr.readyState == 4) { // `DONE` 
         status = xhr.status; 
         if (status == 200) { 
         successHandler && successHandler(xhr.response); 
         } else { 
         errorHandler && errorHandler(status); 
         } 
        } 
        }; 
        xhr.send(); 
    }; 
    
    getJSON('https://mathiasbynens.be/demo/ip', function(data) { 
        alert('Your public IP address is: ' + data.ip); 
    }, function(status) { 
        alert('Something went wrong.'); 
    }); 
    

    閱讀更多:https://mathiasbynens.be/notes/xhr-responsetype-json

相關問題