我想使用json获取api值.如果我点击json按钮,我没有得到任何响应


I want to get the api values using json.if I click the json button I did not get any response

我想使用json获取api值。如果我点击json按钮,我没有得到任何响应,我不知道为什么昨天没有运行,我已经用api方法检查过了,它只是在发布中。我不知道我缺少什么。这是我的代码:

<script type="text/javascript">
function json()
{
    xmlhttp= new XMLHttpRequest();
    var url="http://new.ezeeinfosolutions.com/busservices/auth/getAuthToken?namespaceCode=demo&username=ram@demo.com&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>

我可以假设http://new.ezeeinfosolutions.com"不是你的域,你需要在服务器上创建一些php镜像文件。

此文件将从获得响应http://new.ezeeinfosolutions.com并返回json。

他们几乎没有可能的解决方案,如果他们中的任何一个能让你成功的话:)

  1. 尝试设置响应类型。

    xmlhttp.responseType='json';

  2. 尝试使用xmlhttp.response而不是xmlhttp.reresponseText

  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