2016-11-15 75 views
0

我想使用HTTP GET方法来检索后面这是JSON格式显示在data_id

以下基础数据这里是我的JSON数据HTTP GET方法用于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> 
+0

你在浏览器的开发者工具控制台中看到什么错误? –

+0

'getJSON'在'jQuery'中定义,所以如果你想用'getJSON'使用'jQuery.getJSON'作为起点,请添加jQuery。 – zmii

+0

您也可以使用原生'fetch' API,如https://davidwalsh.name/fetch所述,如果您不想使用jQuery – zmii

回答

-1

你为什么不使用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(数据)函数来显示JSON格式数据....

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

我也搜索关于Ajax的一些信息,但我对Ajax很新,使用起来很舒服吗? –

+0

不要仅仅因为它是jQuery就使用jQuery –

+0

我通常使用ajax请求从数据库获取数据......它的响应很快且易于使用...... –