此JSON请求:
$.ajax({
url:jSONurl+'?orderID='+thisOrderID+'&variationID='+thisVariationID+'&quantity='+thisQuantity+'&callback=?',
async: false,
type: 'POST',
dataType: 'json',
success: function(data) {
if (data.response == 'success'){
//show the tick. allow the booking to go through
$('#loadingSML'+thisVariationID).hide();
$('#tick'+thisVariationID).show();
}else{
//show the cross. Do not allow the booking to be made
$('#loadingSML'+thisVariationID).hide();
$('#cross'+thisVariationID).hide();
$('#unableToReserveError').slideDown();
//disable the form
$('#OrderForm_OrderForm input').attr('disabled','disabled');
}
},
error: function(data){
alert('error');
}
})
在某些情况下会以以下形式带回500错误:
jQuery17205593111887289146_1338951277057({"message":"Availability exhausted","status":500});
然而,这对我来说仍然有用,我需要能够正确处理这个问题。
由于某些原因,当返回此500错误时,我的错误函数未被调用,我只是在firebug中出现“NetworkError:500 Internal Server Error”错误。
我该如何处理?
答案 0 :(得分:30)
您是否尝试过statuscode
回调
$.ajax({
statusCode: {
500: function() {
alert("Script exhausted");
}
}
});
答案 1 :(得分:5)
查看jqXHR Object文档。您可以使用fail方法捕获任何错误。
您的案例如下:
$.post(jSONurl+'?orderID='+thisOrderID+'&variationID='+thisVariationID+'&quantity='+thisQuantity+'&callback=?')
.done(function(data){
if (data.response == 'success'){
//show the tick. allow the booking to go through
$('#loadingSML'+thisVariationID).hide();
$('#tick'+thisVariationID).show();
}else{
//show the cross. Do not allow the booking to be made
$('#loadingSML'+thisVariationID).hide();
$('#cross'+thisVariationID).hide();
$('#unableToReserveError').slideDown();
//disable the form
$('#OrderForm_OrderForm input').attr('disabled','disabled');
}
}, "json")
.fail(function(jqXHR, textStatus, errorThrown){
alert("Got some error: " + errorThrown);
});
我还会考虑通过post传递json数据字符串而不是附加查询变量:
$.post(jSONurl, $.toJSON({orderID: thisOrderID, variationID: thisVariationID, quantity: thisQuantity, callback: false}))
答案 2 :(得分:4)
我认为你可以通过添加这个来抓住它:
$.ajax({
statusCode: {
500: function() {
alert("error");
}
},
url:jSONurl+'?orderID='+thisOrderID+'&variationID='+thisVariationID+'&quantity='+thisQuantity+'&callback=?',
async: false,
type: 'POST',
dataType: 'json',
success: function(data) {
if (data.response == 'success'){
//show the tick. allow the booking to go through
$('#loadingSML'+thisVariationID).hide();
$('#tick'+thisVariationID).show();
}else{
//show the cross. Do not allow the booking to be made
$('#loadingSML'+thisVariationID).hide();
$('#cross'+thisVariationID).hide();
$('#unableToReserveError').slideDown();
//disable the form
$('#OrderForm_OrderForm input').attr('disabled','disabled');
}
},
error: function(data){
alert('error');
}
})
答案 3 :(得分:3)
如果您使用的是POST,可以使用以下内容:
$.post('account/check-notifications')
.done(function(data) {
// success function
})
.fail(function(jqXHR){
if(jqXHR.status==500 || jqXHR.status==0){
// internal server error or internet connection broke
}
});
答案 4 :(得分:1)
我从ajax调用中删除了dataType:json,我能够捕获错误。在这些情况下,我不需要返回的jSON的内容;只知道返回有错误,所以现在就足够了。 Firebug仍然适合他,但我至少能够在出现错误时执行某些操作
$.ajax({
url:'http://example.com/jsonservice/LiftieWeb/reserve?token=62e52d30e1aa70831c3f09780e8593f8&orderID='+thisOrderID+'&variationID='+reserveList+'&quantity='+thisQuantity+'&callback=?',
type: 'POST',
success: function(data) {
if (data.response == 'Success'){
//show the tick. allow the booking to go through
$('#loadingSML'+thisVariationID).hide();
$('#tick'+thisVariationID).show();
}else{
//show the cross. Do not allow the booking to be made
$('#loadingSML'+thisVariationID).hide();
$('#cross'+thisVariationID).hide();
$('#unableToReserveError').slideDown();
//disable the form
$('#OrderForm_OrderForm input').attr('disabled','disabled');
}
},
error: function(data){
alert('error');
}
})