|
| 1 | +# API |
| 2 | + |
| 3 | +## `jQuery.jsonp( options )` |
| 4 | + |
| 5 | +### Overview |
| 6 | + |
| 7 | +Loads a remote JSON object using a JSONP request. |
| 8 | + |
| 9 | +`$.jsonp()` returns the extended options (the `xOptions` object) for the query (that is an object containing all the options passed to the function plus defaults for those not provided). In most cases you won't need `xOptions` to manipulate directly, but it is available if you need to abort the request manually (`xOptions` provides an `abort()` method to that end). |
| 10 | + |
| 11 | +*_As of version 2.3.0, if you use jQuery-JSONP with jQuery 1.5+, the `xOptions` object is a Promise. See [jQuery's documentation](http://api.jquery.com/deferred.promise/) for more information on Promises and Deferreds._* |
| 12 | + |
| 13 | +`$.jsonp()` takes one argument, an object of key/value pairs, that are used to initialize and handle the request. All options are optional. A default can be set for any option with `$.jsonp.setup()`. See below for a full list of the key/values that can be used. |
| 14 | + |
| 15 | +### Options |
| 16 | + |
| 17 | +#### beforeSend - `function` (`undefined`) |
| 18 | + |
| 19 | +A function called before the request is even performed. You may return `false` in the callback to cancel the request. |
| 20 | + |
| 21 | +```js |
| 22 | +function (xOptions) { |
| 23 | + this; // the xOptions object or xOptions.context if provided |
| 24 | +} |
| 25 | +``` |
| 26 | + |
| 27 | +#### cache - `boolean` (`false`) |
| 28 | + |
| 29 | +If set to `false` it will force the data that you request not to be cached by the browser, unless the `pageCache` option is set to true. |
| 30 | + |
| 31 | +#### callback - `string` (`"_jqjsp"`) |
| 32 | + |
| 33 | +Name of the JSONP callback as provided in the request url. Most of the time, you won't have to change this value but some JSONP providers have a predefined callback name that cannot be overidden. Set the name of said callback in the `callback` option and `$.jsonp` will catch the call. |
| 34 | + |
| 35 | +*_As of version 2.0, the library defines a function for the callback in the global scope, so it is no longer safe to use the name of a function already defined: said function will be redefined by jQuery-JSONP._* |
| 36 | + |
| 37 | +*_On the other hand, it is still perfectly safe to have several concurrent requests using the same callback name: in that case, jQuery-JSONP ensures no collision occurs internally._* |
| 38 | + |
| 39 | +#### callbackParameter - `string` (`undefined`) |
| 40 | + |
| 41 | +*_as of version 1.0.1_* |
| 42 | + |
| 43 | +If provided, specifies the name of the url parameter that conveys the callback name to the service. |
| 44 | + |
| 45 | +Example: |
| 46 | + |
| 47 | +```js |
| 48 | +$.jsonp({ |
| 49 | + url: "http://service.org/path?param1=xx¶m2=xx", |
| 50 | + callbackParameter: "callback" |
| 51 | +}); |
| 52 | +``` |
| 53 | + |
| 54 | +is strictly equivalent to: |
| 55 | + |
| 56 | +```js |
| 57 | +$.jsonp({ |
| 58 | + url: "http://service.org/path?param1=xx¶m2=xx&callback=?" |
| 59 | +}); |
| 60 | +``` |
| 61 | + |
| 62 | +#### charset - `string` (`undefined`) |
| 63 | + |
| 64 | +*_as of version 2.1.1_* |
| 65 | + |
| 66 | +Forces the request to be interpreted as a certain charset. Only needed for charset differences between the remote and local content. |
| 67 | + |
| 68 | +#### complete - `function` (`undefined`) |
| 69 | + |
| 70 | +A function to be called when the request finishes (after success and error callbacks are executed). The function gets passed two arguments: The `xOptions` object and a string describing the type of completion of the request (`"success"`, `"error"` or `"timeout"`). |
| 71 | + |
| 72 | +```js |
| 73 | +function (xOptions, textStatus) { |
| 74 | + this; // the xOptions object or xOptions.context if provided |
| 75 | +} |
| 76 | +``` |
| 77 | + |
| 78 | +#### context - `object` (`undefined`) |
| 79 | + |
| 80 | +*_as of version 2.1.1_* |
| 81 | + |
| 82 | +This object will be made the context of all `xOptions`-related callbacks. For example specifying a DOM element as the context will make it the context for the complete callback of a request, like so: |
| 83 | + |
| 84 | +```js |
| 85 | +$.jsonp({ |
| 86 | + url: "test.php?callback=?", |
| 87 | + context: document.body, |
| 88 | + complete: function() { |
| 89 | + $(this).addClass("done"); |
| 90 | + } |
| 91 | +}); |
| 92 | +``` |
| 93 | + |
| 94 | +#### data - `object | string` (`undefined`) |
| 95 | + |
| 96 | +Data to be sent to the server. It is converted to a query string, if not already a string, and then appended to the url. Object must be key/value pairs. If value is an array, jQuery serializes multiple values with same key i.e. `{ foo: [ "bar1", "bar2" ] }` becomes `"&foo=bar1&foo=bar2"`. |
| 97 | + |
| 98 | +#### dataFilter - `function` (`undefined`) |
| 99 | + |
| 100 | +A function to be used to handle the raw responsed JSON object. This is a pre-filtering function to sanitize the response. You should return the sanitized data. The function gets one argument: The raw JSON object returned from the server. |
| 101 | + |
| 102 | +```js |
| 103 | +function (json) { |
| 104 | + // do something |
| 105 | + // return the sanitized json |
| 106 | + return json; |
| 107 | +} |
| 108 | +``` |
| 109 | + |
| 110 | +#### error - `function` (`undefined`) |
| 111 | + |
| 112 | +A function to be called if the request fails. The function is passed two arguments: The `xOptions` object and a string describing the type of error that occurred. Possible values for the second argument are `"error"` (the request finished but the JSONP callback was not called) or `"timeout"`. |
| 113 | + |
| 114 | +```js |
| 115 | +function (xOptions, textStatus) { |
| 116 | + this; // the xOptions object or xOptions.context if provided |
| 117 | +} |
| 118 | +``` |
| 119 | + |
| 120 | +#### pageCache - `boolean` (`false`) |
| 121 | + |
| 122 | +If set to true, will override the `cache` option and provide a page based caching of the result of the request. Whenever another request is made later on with `pageCache` set to `true` and with options inducing the exact same final url for the request, the cached response is used and no distant call is actually made. |
| 123 | + |
| 124 | +Lifetime of this cache is page-based which means it gets erased at each page reload. |
| 125 | + |
| 126 | +#### success - `function` (`undefined`) |
| 127 | + |
| 128 | +A function to be called if the request succeeds. The function gets passed two arguments: The JSON object returned from the server and a string describing the status (always `"success"`). |
| 129 | + |
| 130 | +```js |
| 131 | +function (json, textStatus) { |
| 132 | + this; // the xOptions object or xOptions.context if provided |
| 133 | +} |
| 134 | +``` |
| 135 | + |
| 136 | +#### timeout - `number` (`0`) |
| 137 | + |
| 138 | +If greater than `0`, sets a local timeout (in milliseconds) for the request. This will override the global timeout, if one is set via `$.jsonp.setup`. For example, you could use this property to give a single request a longer timeout than all other requests that you've set to time out in one second. |
| 139 | + |
| 140 | +#### traditional - `boolean` (`false`) |
| 141 | + |
| 142 | +*_as of version 2.0.2_* |
| 143 | + |
| 144 | +Set this to true if you wish to use the traditional style of [param serialization](http://api.jquery.com/jQuery.param/). |
| 145 | + |
| 146 | +#### url - `string` (`location.href`) |
| 147 | + |
| 148 | +The URL to request. If more than one `?` character is found in the url, the latest will be replaced by the value of the `callback` option. Note that, given this rule, no valid url can have more than two `?` characters. |
| 149 | + |
| 150 | +## `jQuery.jsonp.setup( options )` |
| 151 | + |
| 152 | +### Overview |
| 153 | + |
| 154 | +Setup global settings for JSONP requests. |
| 155 | + |
| 156 | +### Options |
| 157 | + |
| 158 | +See $.jsonp for a description of all available options. |
0 commit comments