在html中加载JSON文件

时间:2018-02-04 04:29:36

标签: javascript jquery html json

嘿伙计我在加载json文件时遇到问题。就像当我点击应该加载它的按钮时没有任何反应。 HTML新手,所以任何帮助都将非常感激。这只是函数而不是整个文件!

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" th:replace="~{fragments/layout :: layout (~{::body},'hello')}">
<body style="background-color:green;">
  <div class="container">
    <h1 style="font-family:verdana;"><center>ANAME</center></h1>
    <input type='text' value='Input directory' id = 'input' style='width:200px;margin:0 50%;position:relative;left:-100px;'><br></br>
    <input type='submit' value='Get Data!' id='demo' onClick='myFunction()' style='height:50px;width:100px;margin:0 50%;position:relative;left:-50px;'>


    <script>
    function myFunction() {
        //---my custom validation---
        var inputField = document.getElementById("input");
        if (inputField.value == "" || inputField.value == "Input directory") {
            return;
        }

        //---Loading JSON---
        $.ajax({
            url: "test.json",
            dataType: "json",
            data: {},
            success: function(data){
                //---Do whatever with the loaded 'data' variable;
                alert('go');
            },
            error:function(error){
                alert('Invalid');
            }
        });             
    }
    </script>
  </div>
</body>

</html> 

3 个答案:

答案 0 :(得分:1)

function myFunction() {
        
        var inputField = document.getElementById("input");
        var fr = null;
        if (input.files && input.files[0]) {
            fr = new FileReader();
            fr.onload = function() {
                alert(fr.result);
            };
            fr.readAsText(inputField.files[0]);
        }else
          alert("plz select a file");
        
    }
<input type="file" id="input"/>
<input type="button" onclick="myFunction()" value="test" />

你可以用这个

$.ajax({
  dataType: "json",
  url: url,
  data: data,
  success: function(data){},
  error:function(error){}
});

getJSON是大多数实现都会指定成功处理程序

$.getJSON( "test.json", function( data ) {
  alert("success");
});

更多信息http://api.jquery.com/jquery.getjson/#jQuery-getJSON-url-data-success

修改

从input元素加载json文件,你可以使用这个

    function myFunction() {
 if (!window.File || !window.FileReader || !window.FileList || !window.Blob) {
                alert('The File APIs are not fully supported in this browser.');
                return;
            }   
            var inputField = document.getElementById("input");
            var fr = null;
            if (input.files && input.files[0]) {
                fr = new FileReader();
                fr.onload = function() {
                    alert(fr.result);
                };
                fr.readAsText(inputField.files[0]);
            }
        }

答案 1 :(得分:0)

您可以尝试以下操作:

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" th:replace="~{fragments/layout :: layout (~{::body},'hello')}">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>
<body style="background-color:green;">
<div class="container">
    <h1 style="font-family:verdana;"><center>ANAME</center></h1>
    <input type='text' value='Input directory' id = 'input' style='width:200px;margin:0 50%;position:relative;left:-100px;'><br></br>
    <input type='submit' value='Get Data!' id='demo' onClick='return myFunction();' style='height:50px;width:100px;margin:0 50%;position:relative;left:-50px;'>

<script>
function myFunction() {
    //---my custom validation---
    var inputField = document.getElementById("input");
    if (inputField.value == "" || inputField.value == "Input directory") {
        return false;
    }

    //---Loading JSON---
    $.ajax({
        url: "test.json",
        dataType: "json",
        data: {},
        success: function(data){
            //---Do whatever with the loaded 'data' variable;
            alert('go');
        },
        error:function(error){
            alert('Invalid');
        }
    });
    return false;
}
</script>

</div>
</body>
</html>

答案 2 :(得分:0)

您发布的标记没有指向jQuery库的链接,您可以在标记上方插入此代码以包含它,jQuery使用'$'字符来提高响应速度&amp;更简单的JavaScript。代码上方的代码:

<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>

要从外部源获取json数据,您可以使用XMLHTTTPREQUEST对象从客户端向服务器或其他服务器发送HTTP请求。这个对象的一个​​常见例子如下:

var req = new XMLHttpRequest(); 
req.responseType = 'json'; 
req.open('GET', url, true); 
req.onload = function() { 
   var jsonResponse = req.response; 
   // do something with jsonResponse 
}; 
req.send(null);