HTTP Get Method获取JSON数据

HTTP Get Method For JSON data

本文关键字:JSON 数据 获取 Method Get HTTP      更新时间:2023-09-26

我想使用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无法做到这一点,我的web浏览器没有得到任何响应。我可以知道它是我的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>

为什么不使用ajax直接以JSON格式获得响应…您可以使用下面的代码来实现它。

$.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(data)函数以json格式显示数据....

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