Created
February 27, 2016 17:40
-
-
Save deepakkj/3e9f9be8488f9fc75f93 to your computer and use it in GitHub Desktop.
Adding a new collection/data to JSON using 'POST' method
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<script src="https://code.jquery.com/jquery-1.11.3.js"></script> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width"> | |
<title>Deepak Kumar Jain</title> | |
</head> | |
<body> | |
<h1>Adding a new collection/data to JSON using 'POST' method</h1> | |
<h4>-Using Jquery and REST API</h4> | |
<h2>Adding Friends Detail</h2> | |
<small>Thanks to http://rest.learncode.academy/ for providing the RESTful API </small> | |
<script> | |
//using AJAX to add a new collection/item to the JSON data from the given URL and call respective functions on success/error. | |
//URL used is only for learning/educational purposes. Cannot be used for production. | |
$.ajax({ | |
type: "POST", | |
url: "http://rest.learncode.academy/api/deepak/friends", | |
data: {"name":"GJ","age":"23"}, | |
success: function(data){ | |
console.log("New friend added to Deepak friend list", data); | |
}, | |
error: function(data){ | |
console.log("Error in creating/posting a new collection data"); | |
} | |
}); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
JSBin link: http://jsbin.com/vukevo