Thursday, 19 November 2015

Read JSON file using Ajax

 pincodeBranch.json file contains 

{ "p110001": "Delhi", "p110002": "Delhi"}

 pincodeBranch.js file contains

$.ajax({
    url: "pincodeBranch.json",
    type: "post",
    async: true,
    dataType: "json",
    data: {},
    success: function (data) {
        console.log(data.p110001);
        console.log('json loaded');
    },
    error: function () {
        console.log(" Please refresh: Problem in loading json.");
    }
});


index.html file contains 

<!DOCTYPE html>
<html>
<head>
    <title>Ajax JSON Loading</title>
    <script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
    <script src="pincodeBranch.js"></script>
</head>
<body>
    hi
</body>
</html> 

No comments:

Post a Comment