Hi Friends,
I am come with a new query in jQuery. I have a listing form in which the details showed , the View more Details is implemented using Ajax.
When Click "+" sign there is a loading image show First then the Result show there ... It's all working Fine , But I need a way to show Error
I mean some time Server Response is success with Empty String on that time I need to catch same .... I explain code Below:

if(proceed) {
	      $.ajax({
		  	type: "POST",
		  	data: "key="+id,
		  	dataType: 'json',
		  	async: true,
		  	url: "<?php echo site_url(); ?>/server/view_serverdet/",
		  	beforeSend: function() {
	        	$('#view_load'+id).show();
	        	$('#view_load'+id).addClass(class);
	  		},
	        complete: function(){
                      	
	        		$('#view_load'+id).hide();
	        	}
	        },
		  	success: function (server_msg) {
		  		if (null == server_msg) {
		  			var warning = "<font color=\"red\">Unable to load server details</font>";
                	$('#view_load'+id).html(warning);
				} else {
					
                       // Working Fine!
                               }

But that error Just Show and Hide ... How I can show that Error there, If you have any idea , pls help me ASAP...

Thankfully
Anes P.A :)

Dani AI

Generated

Quick diagnosis: the error text is probably being written into the same element you hide at the end of the request, so it briefly appears then your completion handler hides it again. Also, when you ask for JSON but the server returns an empty body, the request may not reach the success path—so you should handle both the “empty-but-parseable” case and the Ajax error path.

A pragmatic, robust fix:

  • keep the spinner and the details output in separate DOM nodes (spinner inside the container, details in a child div). Hide only the spinner in the final handler, not the whole container.
  • handle both successful-but-empty responses and Ajax failures (use success/done and error/fail).
  • prefer returning valid JSON from the server (even an empty object or a specific error payload) or use HTTP 204 to indicate “no content.”
  • escape any server text before injecting it into the page and use a CSS class for error styling instead of inline tags.

Example (adapt this to your IDs and rendering code):

$.ajax({
  type: 'POST',
  url: '/server/view_serverdet/',
  data: { key: id },
  dataType: 'json'
})
.done(function(data){
  if (!data || ($.isPlainObject(data) && $.isEmptyObject(data))) {
    $('#view_load' + id + ' .details')
      .html('<span class="error">Unable to load server details</span>')
      .show();
  } else {
    // render data into .details
  }
})
.fail(function(jqXHR, textStatus, errorThrown){
  var raw = jqXHR.responseText || errorThrown || textStatus;
  var safe = $('<div/>').text(raw).html(); // escape
  $('#view_load' + id + ' .details').html('<span class="error">Error: ' + safe + '</span>').show();
})
.always(function(){
  $('#view_load' + id + ' .spinner').hide();
});

Notes: ’s content-inspection idea and @developer’s trimming hint point in useful directions, but separating spinner vs. message and explicitly handling the error/fail path fixes the disappearing message reliably. Use the browser Network tab and jqXHR.responseText to see exactly what the server returned during testing.

Recommended Answers

All 2 Replies

try changing: if (null == server_msg) to: if (!/a-z/i.test(server_msg))

Maybe..

if(server_msg.replace(/^\s+|\s+$/g, '') == "null")
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.