Skip to content

Commit e11010e

Browse files
committed
Integrated jQuery docs from #204.
2 parents 52a4f19 + 10a0348 commit e11010e

File tree

1 file changed

+92
-0
lines changed

1 file changed

+92
-0
lines changed

docs/tips.rst

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,95 @@ that happen. You can do this via the ``shouldSendCallback`` setting:
9090
var sampleRate = 10;
9191
return (Math.random() * 100 <= sampleRate);
9292
}
93+
94+
jQuery AJAX Error Reporting
95+
---------------------------
96+
97+
For automatically AJAX errors from jQuery the following tip might come in
98+
helpful. However depending on the type of request you might have to do
99+
slightly different things.
100+
101+
Same Origin
102+
-----------
103+
104+
Whenever an Ajax request completes with an error, jQuery triggers the
105+
``ajaxError`` event, passing the ``event`` object, the ``jqXHR`` object
106+
(prior to jQuery 1.5, the ``XHR`` object), and the ``settings`` object
107+
that was used in the creation of the request. When an HTTP error occurs,
108+
the fourth argument (``thrownError``) receives the textual portion of the
109+
HTTP status, such as "Not Found" or "Internal Server Error."
110+
111+
You can use this event to globally handle Ajax errors:
112+
113+
.. code-block:: javascript
114+
115+
$(document).ajaxError(function(event, jqXHR, ajaxSettings, thrownError) {
116+
Raven.captureMessage(thrownError || jqXHR.statusText, {
117+
extra: {
118+
type: ajaxSettings.type,
119+
url: ajaxSettings.url,
120+
data: ajaxSettings.data,
121+
status: jqXHR.status,
122+
error: thrownError || jqXHR.statusText,
123+
response: jqXHR.responseText.substring(0, 100)
124+
}
125+
});
126+
});
127+
128+
129+
**Note:**
130+
131+
* This handler is not called for cross-domain script and cross-domain
132+
JSONP requests.
133+
* If ``$.ajax()`` or ``$.ajaxSetup()`` is called with the ``global``
134+
option set to ``false``, the ``.ajaxError()`` method will not fire.
135+
* As of jQuery 1.8, the ``.ajaxError()`` method should only be attached to
136+
document.
137+
138+
139+
Cross Origin
140+
------------
141+
142+
Due security reasons most web browsers are not giving permissions to
143+
access error messages for cross domain scripts. This is not jQuery issue
144+
but an overall javascript limitation.
145+
146+
Depending on your situation you have different options now:
147+
148+
When you control the backend
149+
````````````````````````````
150+
151+
If you have access to the backend system you are calling, you can set
152+
response headers to allow a cross domain call:
153+
154+
.. code-block:: yaml
155+
156+
Access-Control-Allow-Origin: http://domain1.com, http://domain2.com
157+
158+
Script tags have now got a new non-standard attribute called
159+
``crossorigin`` (`read more
160+
<https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script#attr-crossorigin>`_).
161+
The most secure value for this would be ``anonymous``. So, you'll have to
162+
modify your script tags to look like the following:
163+
164+
.. code-block:: html
165+
166+
<script src="http://sub.domain.com/script.js" crossorigin="anonymous"></script>
167+
168+
When you have no access to the backend
169+
``````````````````````````````````````
170+
171+
If you have no access to the backend, you could try a workaround, which is
172+
basically adding a timeout on the Ajax call. This is however very dirty,
173+
and will fail on slow connection or long response time:
174+
175+
.. code-block:: javascript
176+
177+
$.ajax({
178+
url: 'http:/mysite/leaflet.js',
179+
success: function() { ... },
180+
error: function() { ... },
181+
timeout: 2000, // 2 seconds timeout before error function will be called
182+
dataType: 'script',
183+
crossDomain: true
184+
});

0 commit comments

Comments
 (0)