Reputation: 451
What I want to ask is that I have a jqgrid list and i want to get error message on screen in case deletion fails. Here is my code:
<html>
<body>
<script type="text/javascript" charset="utf-8">
$(document).ready(function() {
jQuery("#list").jqGrid({
url:'../listeAjax',
datatype: "json",
colNames:['Id','Hizmet adı', 'Sektörler', 'Durum', 'Düzenle'],
colModel:[
{name:'onto_data_id',index:'onto_data_id', width:55, editable: false},
{name:'baslik',index:'baslik', editable:true, editrules:{required:true}, formoptions:{ elmprefix:"(*)"}},
{name:'sektor_list',index:'sektor_list', editable:false},
{name:'is_active',index:'is_active', align: 'center', editable:true, stype:'select',
editoptions:{value:":All;1:<?php echo lang('title_aktif')?>;0:<?php echo lang('title_pasif')?>", defaultValue:1},
edittype:"select"
},
{name:'duzenle',index:'duzenle', align: 'center', sortable:false, search: false, editable:false}
],
rowNum: <?php echo DEFAULT_GRID_ROWNUM; ?>,
rowList:[100,200,400,800],
pager: '#pager',
sortname: 'onto_data_id',
viewrecords: true,
sortorder: "desc",
multiselect: true,
width: 800,
height: "100%",
editurl: '../islemAjax',
caption: "Hizmetler",
mtype: "GET",
postData:{parent_id: '<?php echo $parent_id; ?>', parent_type: '<?php echo $parent_type; ?>'}
});
jQuery("#list").jqGrid('navGrid','#pager',
{edit:true, add:true, del:true, search:false},
{closeOnEscape:true, bottominfo:"<?php langValue('info_yildizlialanlar'); ?>",editData: {
parent_id: function() {
return '<?php echo $parent_id; ?>';
},
parent_type: function() {
return '<?php echo $parent_type; ?>';
},
tur: function() {
return '<?php echo ONTO_TYPE_HIZMET; ?>';
}
}},
{
beforeSubmit: function(postdata, formid){
var message;
$.ajax( {
type : 'POST',
dataType : 'json',
url : "<?php echo URL_YONETIMDATACONTROL; ?>",
data : 'baslik='+postdata.baslik + '&parent_id=' + <?php echo $parent_id; ?> + '&parent_type=' + <?php echo $parent_type; ?> + '&onto_type=' + <?php echo ONTO_TYPE_HIZMET ; ?>,
async : false,
success : function(result) {
if(result.status == true) {
complete = true;
} else {
complete = false;
}
message = result.message;
},
error: function (data, status, e) {
complete = false;
message = e;
}
});
return [complete, message, ""];
},
closeOnEscape:true, bottominfo:"<?php langValue('info_yildizlialanlar'); ?>",editData: {
parent_id: function() {
return '<?php echo $parent_id; ?>';
},
parent_type: function() {
return '<?php echo $parent_type; ?>';
},
tur: function() {
return '<?php echo ONTO_TYPE_HIZMET; ?>';
}
}},
{delData: {
parent_id: function() {
return '<?php echo $parent_id; ?>';
},
parent_type: function() {
return '<?php echo $parent_type; ?>';
}
}},
{}
);
jQuery("#list").jqGrid('filterToolbar');
} );
</script>
<h1>
<?php echo langValue('title_hizmetliste'); ?>
<?php
if(isset($sektor_baslik))
{
echo '(' . $sektor_baslik . ')';
}
?>
<a href="<?php echo URL_YONETIMHIZMETEKLE; ?>" title="<?php echo langValue('form_ekle')?>" class="tooltip">
<img src="<?php echo PATH_MEDIA_IMG_YONETIM; ?>icons/8_48x48.png" alt="" />
</a>
</h1>
<div class="pad20">
<?php if(isset($info)) { ?>
<div class="message success close">
<h2><?php echo $info; ?></h2>
</div>
<?php } ?>
<!-- Tabs -->
<div id="tabs">
<!-- First tab -->
<div id="tabs-1">
<table id="list"></table>
<div id="pager"></div>
</div>
</div>
</div>
</body>
</html>
I inserted afterSubmit but it returns nothing. Moreover, when I alert a text in afterSubmit, nothing happens. Here is the aftersubmit code I tried:
afterSubmit : function(response, postdata)
{
alert("test");
return [success,message,new_id]
}
What is wrong in here?
Upvotes: 0
Views: 1205
Reputation: 222017
There are two main cases in error handling of the server response: 1) the response contain some error HTTP code and 2) the response contains some successful HTTP code. In case of an successful response (from the HTTP code point of view) the callback afterSubmit
will be executed. In case of an error response (from the HTTP code point of view) the callback errorTextFormat
will be executed. So you need use something like
{
delData: {
parent_id: function() {
return '<?php echo $parent_id; ?>';
},
parent_type: function() {
return '<?php echo $parent_type; ?>';
}
},
errorTextFormat: function (jqXHR) {
alert(jqXHR.responseText);
return "Error code: " + data.status + ", Description: " + jqXHR.responseText;
}
}
The exact code of errorTextFormat
depend on the content of the jqXHR.responseText
in case of error. In many cases it's better to use only subset of jqXHR.responseText
to have the mostly readable results. See the answer.
I would recommend you to test always not only the case where the error response will be explicitly produced by your code, but two additional cases: 1) common server error (for example if you would use non-existing url
for delete operation) 2) client side error like timeout from the server.
Upvotes: 1