Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

ajax error

$.ajax({
    type: "post", url: "/SomeController/SomeAction",
    success: function (data, text) {
        //...
    },
    error: function (request, status, error) {
        alert(request.responseText);
    }
});
Comment

ajax jquery errors

$.ajax({
    url: 'some_unknown_page.html',
    success: function (response) {
        $('#post').html(response.responseText);
    },
    error: function (jqXHR, exception) {
        var msg = '';
        if (jqXHR.status === 0) {
            msg = 'Not connect.
 Verify Network.';
        } else if (jqXHR.status == 404) {
            msg = 'Requested page not found. [404]';
        } else if (jqXHR.status == 500) {
            msg = 'Internal Server Error [500].';
        } else if (exception === 'parsererror') {
            msg = 'Requested JSON parse failed.';
        } else if (exception === 'timeout') {
            msg = 'Time out error.';
        } else if (exception === 'abort') {
            msg = 'Ajax request aborted.';
        } else {
            msg = 'Uncaught Error.
' + jqXHR.responseText;
        }
        $('#post').html(msg);
    },
});
Comment

ajax error exception handeling

from django.conf import settings
from django.http import HttpResponse, Http404
from django.db.models.base import ObjectDoesNotExist
from django.utils.translation import ugettext as _

from snippet 800 import JSONEncoder
json_encode = JSONEncoder().encode

class AjaxErrorMiddleware(object):
    '''Return AJAX errors to the browser in a sensible way.

    Includes some code from http://www.djangosnippets.org/snippets/650/
    '''

    # Some useful errors that this middleware will catch.
    class InputError(Exception):
        def __init__(self, message):
            self.message = message

    class ParameterMissingError(InputError):
        def __init__(self, param):
            super(AjaxErrorMiddleware.ParameterMissingError, self).__init__(
                _('Required parameter missing: %s') % param)


    def process_exception(self, request, exception):
        if not request.is_ajax(): return

        if isinstance(exception, (ObjectDoesNotExist, Http404)):
            return self.not_found(request, exception)

        if isinstance(exception, AjaxErrorMiddleware.InputError):
            return self.bad_request(request, exception)

        return self.server_error(request, exception)
    

    def serialize_error(self, status, message):
        return HttpResponse(json_encode({
                    'status': status,
                    'message': message}),
                            status=status)

    
    def not_found(self, request, exception):
        return self.serialize_error(404, str(exception))

    
    def bad_request(self, request, exception):
        return self.serialize_error(400, exception.message)


    def server_error(self, request, exception):
        if settings.DEBUG:
            import sys, traceback
            (exc_type, exc_info, tb) = sys.exc_info()
            message = "%s
" % exc_type.__name__
            message += "%s

" % exc_info
            message += "TRACEBACK:
"    
            for tb in traceback.format_tb(tb):
                message += "%s
" % tb
            return self.serialize_error(500, message)
        else:
            return self.serialize_error(500, _('Internal error'))
Comment

PREVIOUS NEXT
Code Example
Javascript :: yarn react-redux 
Javascript :: jquery find if select option exists 
Javascript :: react native rename 
Javascript :: jquery delay 1 second 
Javascript :: load lodash chrome console 
Javascript :: How to get the browser to navigate to a URL in JavaScript 
Javascript :: javascript how to remove accents or diacritics 
Javascript :: jquery selector input by type 
Javascript :: js countKeys 
Javascript :: react native textInput numbers only 
Javascript :: dconf-editor install 
Javascript :: react 18 createroot 
Javascript :: javascript get current url 
Javascript :: error /node_modules/node-sass: Command failed 
Javascript :: jquery phone number mask 
Javascript :: jquery visibility hidden show 
Javascript :: javascript replace newline 
Javascript :: function time javascript 
Javascript :: drupal 8 get page node 
Javascript :: create random 4 digit number js 
Javascript :: javascript remove spaces at the beginning of the end of the string 
Javascript :: select first option in dropdown jquery 
Javascript :: list of currencies with code js 
Javascript :: how to get the height of window in javascript 
Javascript :: addeventlistener hover js 
Javascript :: node write text to file 
Javascript :: copy a file and paste with fs 
Javascript :: jquery check if div has a certain style 
Javascript :: get height element use js 
Javascript :: go to page jquery 
ADD CONTENT
Topic
Content
Source link
Name
1+2 =