jquery ajax

<script src="<%=request.getContextPath() %>/source/js/jquery-2.0.0.min.js"></script>

<script type="text/javascript">

/**
* 异步加载数据,并执行回调方法
* @param url 调用地址
* @param config 参数,如{teamId : 10, groups : 1}
* @param fuc 回调方法
* @param arg0 回调方法的参数
*/
function fnc_ajax_cso(url, config, fuc, arg0) {
var str = $.param(config, true);
$.ajax({
url : url,
type : "post",
dataType : 'json',
async : true,
cache : false,
data : str,
success : function(json) {
eval("fuc(json, arg0)");
},
error : function(XMLHttpRequest, textStatus){
g_alert('error', '请求失败!');
}
});
}


function fnc_set(json){
if(json.errorCode == -1){ // 失败
$('#testParentDiv').append("<div class=\"item\">"+json.data+"</div>");
$('#testParentDiv').css("height", "auto");
}else if(json.errorCode == 0){
var data = JSON.parse(json.data);

$.each(data, function(ids, item){
if(item.StatusCode == -1){
$('#testParentDiv').append("<div><div class=\"tabdiv1\">No." + (ids+1) + "</div><div class=\"tabdiv2\" style=\"color: red;\">" + item.StatusDescription + "</div></div>");
}else{
$('#testParentDiv').append("<div><div class=\"tabdiv1\">No." + (ids+1) + "</div><div class=\"tabdiv2\" style=\"color: green;\">" + item.StatusDescription + "</div></div>");
}
});
$('#testParentDiv').css("height", "auto");
}

$('#loadingImg').hide();
}

function testProblem(){
$('#loadingImg').show();
$('#testParentDiv').empty();
var config = {
};
fnc_ajax_cso("<%=request.getContextPath() %>/problem/test", config, fnc_set);
}

</script>
<style type="text/css">
.tabdiv1 {
float: left;
width: 5%;
text-align: right;
padding-right: 2%;
height: 35px;
line-height: 35px;
font-size: 14px;
border-bottom: 1px solid black;
}

.tabdiv2 {
float: left;
width: 93%;
text-align: left;
height: 35px;
line-height: 35px;
font-size: 14px;
border-bottom: 1px solid black;
}
</style>

</head>

<body>

<div style="line-height: 50px;">
<input type="button" value="URL测试" onclick="testProblem()" />
<img id="loadingImg" style="display: none;" alt="请稍后..." src="<%=request.getContextPath() %>/source/images/loading.gif">
</div>

<div id="testParentDiv" style="border: 1px solid grey;width: 90%;margin-top: 10px;height: 200px;">

</div>

</body>

posted @ 2017-02-28 09:09  天才与勤奋  阅读(121)  评论(0)    收藏  举报