Reputation: 183
The below code is executing/sending data on body load but i want to make it onclick like
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script src="http://code.jquery.com/jquery-1.10.2.min.js" ></script>
<script>
$.getJSON("http://ip-api.com/json/?callback=?", function(data) {
var table_body = "";
$.each(data, function(k, ip) {
save_info = ip +","+location.hostname+",var1,var2";
/* save_info= ip +","+location.hostname+",var1,var2"; */
});
$(document).ready(function() {
var json_object = {"data": save_info};
$.ajax({
url: "http://www.example.com/test/data.php",
data: json_object,
dataType: 'json',
type: 'POST',
success: function(json_object) {
console.log(json_object);
$("#saved").text("Data has been saved.");
},
error: function(json_object) {
console.log(json_object);
$("#saved").text("Failed to save data !");
}
});
})
});
</script>
</head>
<body>
<a href="#" onclick='getJSON()'> click here</a>
<!-- I want to make json send data when this click event happens, suggest how to write onclick here -->
</body>
</html>
Please also suggest me if there is any alternative way using jquery/javascript to record this user's IP address & hostname, I want to send them to a remote page as parameters where it'll be saved.
Upvotes: 0
Views: 1519
Reputation: 20740
getJSON() is not a function in itself. You never declared the function.
Do this:
function getJSON() {
$.getJSON("http://ip-api.com/json/?callback=?", function(data) {
var table_body = "";
$.each(data, function(k, ip) {
save_info = ip +","+location.hostname+",var1,var2";
});
});
}
Upvotes: 1