我有一系列的链接:
<a href="#">insert into database1</a>
<a href="#">insert into database2</a>
<a href="#">insert into database3</a>
我希望能够点击链接,然后链接将是一个动作插入的东西到所需的数据库(链接的行为就像一个按钮)。
类似如下:
$('a').click(function() {
var link = $(this);
$.ajax({
url: link.attr('href'),
type: 'POST',
dataType: "json",
error: function(req, resulttype, exc)
{
console.log('Something went wrong with the request');
},
success: function(data)
{
if (data.result == 'success') {
console.log('Data added to database');
} else {
console.log('Couldnt add to database');
}
}
});
return false;
});
你可以在需要的地方修改参数。
后端会像这样(PHP示例)。
// do database stuff
print(json_encode(array('result'=>'success')));