Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between dataType jsonp and JSON

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

like image 331
Alizain Prasla Avatar asked Jun 01 '12 14:06

Alizain Prasla


People also ask

What is difference between JSON and JSONP?

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.

What is JSONP dataType?

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.

Is JSONP insecure?

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.

How do I get JSONP data?

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.


2 Answers

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

like image 121
thecodeparadox Avatar answered Oct 17 '22 20:10

thecodeparadox


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.

like image 41
Jeff LaFay Avatar answered Oct 17 '22 20:10

Jeff LaFay