erknrio
11/9/2015 - 6:10 PM

jQuery AJAX snippet.

jQuery AJAX snippet.

function CustomException(text, num) {
  if (typeof text !== "string" || text === null || text === undefined) {
    text = '';
  }

  if (typeof num !== "number" || num === null || num === undefined) {
    num = 0;
  }

  this.message = text;
  this.code = num;
}

var error_code = 0;

$.ajax('your_url_here', {
    "method": 'GET',
    "data": $.param({}),
    "dataType": 'json',
    "timeout": 30000 // 30 seconds
}).done(function (response) {
    try {
        if (response.ok) {
            window.console.log(response);
        } else {
            error_code++;
            throw new CustomException(response.error, error_code);
        }
    } catch (err) {
        throw new Error('Error: ' + err);
    }
}).fail(function (jqXHR, textStatus, errorThrown) {
    throw new Error(textStatus + ': ' + errorThrown);
});