I download Jquery UI autoload, looking to remote-jsonp.html. This is ajax function but i open console.. I can't see any request in my console...
What is difference between dataType;"jsonp" and dataType;"JSON"
$( "#city" ).autocomplete({
source: function( request, response ) {
$.ajax({
url: "http://ws.geonames.org/searchJSON",
dataType: "jsonp",
data: {
featureClass: "P",
style: "full",
maxRows: 12,
name_startsWith: request.term
},
success: function( data ) {
response( $.map( data.geonames, function( item ) {
return {
label: item.name + (item.adminName1 ? ", " + item.adminName1 : "") + ", " + item.countryName,
value: item.name
}
}));
}
});
},
Reference http://jqueryui.com/demos/autocomplete/remote-jsonp.html
JSONP stands for JSON with Padding. Requesting a file from another domain can cause problems, due to cross-domain policy. Requesting an external script from another domain does not have this problem. JSONP uses this advantage, and request files using the script tag instead of the XMLHttpRequest object.
dataType: jsonp for cross-domain request, that means request to different domain and dataType: json for same domain-same origin request. Loads in a JSON block using JSONP. Adds an extra "? callback=?" to the end of your URL to specify the callback.
JSONP is vulnerable to the data source replacing the innocuous function call with malicious code, which is why it has been superseded by cross-origin resource sharing (available since 2009) in modern applications.
Method to use JSONP:In HTML code, include the script tag. The source of this script tag will be the URL from where the data to be retrieve. The web services allow to specify a callback function. In the URL include the callback parameter in the end.
dataType: jsonp
for cross-domain request, that means request to different domain and dataType: json
for same domain-same origin request.
Loads in a JSON block using JSONP. Adds an extra "?callback=?" to the end of your URL to specify the callback. Disables caching by appending a query string parameter, "_=[TIMESTAMP]", to the URL unless the cache option is set to true.
Read about same origin policy
Read more about jQuery AJAX
With JSONP you shouldn't see an ajax request if that's what you're looking for. You should however see a request for the resource because JSONP is used for cross domain calls to pull in data from different domains.
It returns your JSON data wrapped in a function name. jQuery handles the function name behind the scenes and passes the data into your success handler. The data is loaded by dynamically creating a script element with the src attribute pointing to the service being called and then attached to the browser's DOM. Then the browser makes a request to the resource and the web service responds with the callback function and data.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With