jquery中的 ajax 的写法
程序员文章站
2022-07-15 14:35:45
...
2个星期没用jquery,就把ajax写法忘得差不多了,特意这里记录一下。
<div class="container" style="margin-top: 10%;">
<input id="synDepartment" class="synbutton" type="button" value="同步部门" onclick="synDepartment();">
<input id="synHrmResource" class="synbutton" type="button" value="同步人员" onclick="synHrmResource();">
<input id="synJobtitle" class="synbutton" type="button" value="同步岗位" onclick="synJobtitle();">
<input type="button" class="synbutton" value="一键同步" onclick="synAll();">
</div>
<script type="text/javascript">
function synDepartment(){
$('#synDepartment').click(function(){
$.ajax({
type: 'GET',
url:'synDepartment',
success:function(result){
alert(result.message);
}
});
});
}
function synHrmResource(){
$('#synHrmResource').click(function(){
$.ajax({
type:'GET',
url:'synHrmResource',
success:function(result){
alert(result.message);
}
});
});
}
下面是完整的写法:
$(function(){
$('#send').click(function(){
$.ajax({
type: "GET",
url: "test.json",
data: {username:$("#username").val(), content:$("#content").val()},
dataType: "json",
success: function(data){
$('#resText').empty(); //清空resText里面的所有内容
var html = '';
$.each(data, function(commentIndex, comment){
html += '<div class="comment"><h6>' + comment['username']
+ ':</h6><p class="para"' + comment['content']
+ '</p></div>';
});
$('#resText').html(html);
}
});
});
});