HTTP获取JSON数据的方法

时间:2016-11-15 04:07:13

标签: javascript json

我想使用http get方法检索JSON格式的数据,如下图所示基于data_id

这是我的JSON数据

{
    "data_id": "61dffeaa728844adbf49eb090e4ece0e",
    "file_info": {
        "display_name": "samplefile.txt",
        "file_size": 81035,
        "file_type": "text/plain",
        "file_type_description": "ASCII text",
        "md5": "c05017f68343a5257fc3c0db72aa58dc",
        "sha1": "ba46b945f408cc729458380350b4e78f61741c81",
        "sha256": "8805777d2d561255edcb499f7445ef0216b75737bacb6bc6665dbf9830272f53",
        "upload_timestamp": "2015-08-14T12:46:59.360Z"
    },
    "scan_results": {
        "data_id": "61dffeaa728844adbf49eb090e4ece0e",
        "progress_percentage": 100,
        "scan_all_result_a": "No Threat Detected",
        "scan_all_result_i": 0,
        "scan_details": {
            "Engine1": {
                "def_time": "2015-08-13T09:32:48.000Z",
                "location": "local",
                "scan_result_i": 0,
                "scan_time": 1,
                "threat_found": ""
            },
            "Engine2": {
                "def_time": "2015-08-10T00:00:00.000Z",
                "location": "local",
                "scan_result_i": 0,
                "scan_time": 3,
                "threat_found": ""
            }
        },
        "start_time": "2015-08-14T12:46:59.363Z",
        "total_avs": 2,
        "total_time": 389
    },
    "process_info": {
        "post_processing": {
            "actions_ran": "",
            "actions_failed": "",
            "converted_to": "",
            "copy_move_destination": "",
            "converted_destination": ""
        },
        "progress_percentage": 100,
        "user_agent": "webscan",
        "profile": "File scan",
        "result": "Allowed",
        "blocked_reason": "",
        "file_type_skipped_scan": false
    }
}

根据网站文档,我可以通过http://www.example.com/file/ {data_id}检索JSON数据。但是我的JavaScript没有做到这一点,我没有得到任何对我的网络浏览器的响应。我知道我的JavaScript有一些问题需要检索JSON数据吗?我对JSON数据的工作原理有点新鲜

<html>
<head>
  <script>
    function formShow()
     {
      var getData = function(url, callback) {
      var request = new XMLHttpRequest();
      request.open('get', url, true);
      request.responseType = 'json';
      request.onload = function() {
      var status = request.status;
      if (status == 200) {
        callback(null, request.response);
      } else {
        callback(status);
      }
    };
    xhr.send();
  };
  getData('http://192.168.0.25:8008/file/d81d2e183dbd4303a1ffa6d8388bbd27', function(err, data) {
  if (err != null) {
    alert('Something went wrong: ' + err);
  } else {
    alert('Your Json result is:  ' + data.result);
    result.innerText = data.result;
  }
});
     }

  </script>
</head>
<body>
<form method="POST" action="" name="myForm">
  <input type="submit" value="Show" onclick="formShow()">
</form> 
<div id="result" style="color:red"></div>
</body>
</html>

1 个答案:

答案 0 :(得分:-1)

为什么不使用ajax直接在JSON FORMAT中获取响应... 你可以使用以下代码来实现它。

$.ajax({
        type:'GET',
        url: getRootURL() + 'yourpath/methodPath',
        dataType: 'json', // data type of response  
        contentType:'application/json',
        success: populateAllJSONData,
        error: function(jqXHR, textStatus, errorThrown){
            handleErrors('Error: ' , textStatus , errorThrown);
            hideLoader();   
        }
    });

//然后定义populateAllJSONData(数据)函数以json格式显示数据....

function populateAllJSONData(data)
{
   // here "data" contains your responsed JSON output....
}