John Doe
John Doe

Reputation: 1

Loading JSON File in html

Hey guys I've been having a problem loading json file. Like nothing happens when I click the button which should load it up. New to HTML so any help would be very appreciated. This is just the function not the whole file!

<!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> 

Upvotes: 0

Views: 1475

Answers (3)

ali zarei
ali zarei

Reputation: 1241

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" />

you can use this

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

getJSON is most implementations will specify a success handler

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

more information http://api.jquery.com/jquery.getjson/#jQuery-getJSON-url-data-success

EDIT

for loading json file from input element you can use this

    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]);
            }
        }

Upvotes: 1

Reza Mamun
Reza Mamun

Reputation: 6189

You can try with the following:

<!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>

Upvotes: 0

Phil
Phil

Reputation: 321

The markup you posted does not have a link to the jQuery library you can insert this code above the tag to include it, jQuery uses the '$' character to make more responsive & simpler JavaScript. Code above the tag:

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

To fetch json data from an external source you can use the XMLHTTTPREQUEST object to send a HTTP request to the server or other server from the client. A common example of this object is below:

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);

Upvotes: 0

Related Questions