下面的代码正在执行/发送有关正文加载的数据,但我希望像
一样进行onclick<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>
如果有任何替代方法,请使用jquery / javascript记录此用户的IP地址&amp; hostname,我想将它们作为参数发送到远程页面,它将被保存。
答案 0 :(得分:1)
getJSON()本身不是一个函数。你从未宣布过这个功能。
这样做:
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";
});
});
}