|
| 1 | +jQuery |
| 2 | +====== |
| 3 | + |
| 4 | +Automatically catching all AJAX errors |
| 5 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 6 | + |
| 7 | +Same Origin |
| 8 | +----------- |
| 9 | + |
| 10 | +Whenever an Ajax request completes with an error, jQuery triggers the ``ajaxError`` event, passing the ``event`` object, the |
| 11 | +``jqXHR`` object (prior to jQuery 1.5, the ``XHR`` object), and the ``settings`` object that was used in the creation of the |
| 12 | +request. When an HTTP error occurs, the fourth argument (``thrownError``) receives the textual portion of the HTTP status, |
| 13 | +such as "Not Found" or "Internal Server Error." |
| 14 | + |
| 15 | +You can use this event to globally handle Ajax errors: |
| 16 | + |
| 17 | +.. code-block:: javascript |
| 18 | +
|
| 19 | + $(document).ajaxError(function (event, jqXHR, ajaxSettings, thrownError) { |
| 20 | + Raven.captureMessage( |
| 21 | + thrownError || jqXHR.statusText, |
| 22 | + { |
| 23 | + extra: { |
| 24 | + type: ajaxSettings.type, |
| 25 | + url: ajaxSettings.url, |
| 26 | + data: ajaxSettings.data, |
| 27 | + status: jqXHR.status, |
| 28 | + error: thrownError || jqXHR.statusText, |
| 29 | + response: jqXHR.responseText.substring(0, 100) |
| 30 | + } |
| 31 | + } |
| 32 | + ); |
| 33 | + }); |
| 34 | +
|
| 35 | +
|
| 36 | +**Note:** |
| 37 | + |
| 38 | +* This handler is not called for cross-domain script and cross-domain JSONP requests. |
| 39 | + |
| 40 | +* If ``$.ajax()`` or ``$.ajaxSetup()`` is called with the ``global`` option set to ``false``, the ``.ajaxError()`` method will not fire. |
| 41 | + |
| 42 | +* As of jQuery 1.8, the ``.ajaxError()`` method should only be attached to document. |
| 43 | + |
| 44 | + |
| 45 | +Cross Origin |
| 46 | +------------ |
| 47 | + |
| 48 | +Due security reasons most web browsers are not giving permissions to access error messages for cross domain scripts. This is not jQuery issue but an overall javascript limitation. |
| 49 | + |
| 50 | +When you control the backend |
| 51 | +............................ |
| 52 | + |
| 53 | +If you have access to the backend system you are calling, you can set response headers to allow a cross domain call: |
| 54 | + |
| 55 | +.. code-block:: yaml |
| 56 | +
|
| 57 | + Access-Control-Allow-Origin: http://domain1.com, http://domain2.com' |
| 58 | +
|
| 59 | +Script tags have now got a new non-standard attribute called ``crossorigin`` (`read more <https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script#attr-crossorigin>`_). The most secure value for this would be ``anonymous``. So, you'll have to modify your script tags to look like the following: |
| 60 | + |
| 61 | +.. code-block:: html |
| 62 | + |
| 63 | + <script src="http://sub.domain.com/script.js" crossorigin="anonymous"></script> |
| 64 | + |
| 65 | +When you have no access to the backend |
| 66 | +...................................... |
| 67 | + |
| 68 | +If you have no access to the backend, you could try a workaround, which is basically adding a timeout on the Ajax call. This is however very dirty, and will fail on slow connection or long response time: |
| 69 | + |
| 70 | +.. code-block:: javascript |
| 71 | +
|
| 72 | + $.ajax({ |
| 73 | + url: 'http:/mysite/leaflet.js', |
| 74 | + success: function() { ... }, |
| 75 | + error: function() { ... }, |
| 76 | + timeout: 2000, // 2 seconds timeout before error function will be called |
| 77 | + dataType: 'script', |
| 78 | + crossDomain: true |
| 79 | + }); |
| 80 | +
|
0 commit comments