[英]check success and error in ajax
我在這樣的jsp中有一個按鈕。
<input class="submit_button" type="submit" id="btnPay" name="btnPay" value="Payment"
style="position: absolute; left: 350px; top: 130px;" onclick="javascript:payment();">
javascript函數調用java servlet,當單擊按鈕時,servlet調用函數“callprocedure()”。
<script>
function payment()
{
var req = $.ajax({
type: "POST",
url: '/context/Servlet',
success: function(result){
//when successfully return from your java
alert('Payment done successfully...');
}
}, error: function(){
// when got error
alert("sorry payment failed");
}
});
}
</script>
現在一切正常但我的問題是檢查ajax中的成功或錯誤。 在我的情況下,如何檢查ajax中的成功或錯誤。
提前完成了...
您正在這樣做,但您的代碼中有語法錯誤:
function payment() {
var req = $.ajax({
type: "POST",
url: '/context/Servlet',
success: function (result) {
// alert data returned by jsp
alert(result);
alert('Payment done successfully...');
},
error: function (jqXHR,textStatus,errorThrown) {
// when got error
alert("sorry payment failed :" + errorThrown);
}
});
}
您可以使用傳遞給錯誤回調的參數來了解錯誤原因
更多信息
另一種方法(雖然效率不高):
檢查servlet返回的值。
$.post('Servlet',paramName:paramValue,...},function(result) {
if (result.substring(0,...)=='(expected result)'){ //success
}else{ //error
}
});
希望能幫助到你
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.