Search code examples
javascriptphpjqueryajaxjquery-forms-plugin

XHR Object's message is undefined but shows the correct status


I'm making an ajax call that the response would be a 400 http error code. I'm using php as the serverside language. My php :

header('Content-Type: text/html',true,400);
echo $this->upload->display_errors('<p>', '</p>'); 

my javascript:

$('#upload-form').ajaxForm({
            success: function() {
                $(".loader").hide();
                return false;
            },
            complete: function(data) {
                var imageUrls = new Array();
                var i = 0;
                jQuery.each(data, function(index, item) {
                    imageUrls[i] = item.name.toString();
                    i++;
                });
                alert(imageUrls);
                $(".loader").hide();
                return false;
            },
            error : function(xhr, status, errorMessage) {
                //alert(xhr.status);
                alert(status);
                alert(errorMessage);
                //$("#status").html(xhr.message);
                $(".loader").hide();
            }
        });

when I want to show error message, it returns undefined. I've used xhr and xhr.message but no luck. How can I return an error code with a message to an ajax call and proccess the response?


Solution

  • Try this ?

           error: function (xhr, textStatus, errorThrown) {
                            switch (xhr.status) {
                                case 400:
                                case 401:
                                case 402:
                                case 403:
                                case 404:
                                case 500:
                                    message = xhr.responseText;
                                    break;
                                default :
                                    message = 'Error unKnown';
                            }
           }