How do I use JSONP and how do I make a REST web service that supports JSONP output?

I have a project of creating a mobile web application using jQuery Mobile. I want my application to consume data from a cross domain REST web service. Many have said using JSONP. but I'm still confused how to use it and how to create a REST webservice that supports JSONP output. Can anyone help me explain or explain how JSONP works? Or does anyone have a sample code for this case? Thanks earlier.

+3


source to share


2 answers


JSONP is a client side request where the REST service will be a server side request (only if you try to execute an api firewall service). Both are different in terms of process.

JSONP:

You can implement JSONP with javascript or with jQuery ajax. Jquery implementation is the easiest way. JSONP is a type that dynamically includes a javascript script in your html document. This way you can call the js function dynamically.

There are many resources for implementing JSONP. i link below,

Check it out for JSONP implementation



Below is a simple jquery ajax function that triggers a jsonp request for logging and latitude.

$.ajax({
        type: "GET",
        dataType: "jsonp",
        cache: false,
        url: 'http://api.ipinfodb.com/v3/ip-city/?key=b518527dd751af36c974e0adcdc4cb329917df46c006a72bf92858dd7c059488&ip=<?=$client_ip?>&format=json',
        success: function(data) {           
            alert(data.latitude+":"+data.longitude)                             
        },
        error: function(){
            alert('Could not able to find location!');
        }
    });

      

Check this article for REST implementation

Hope this helps you.

+1


source


There is no general agreement on what constitutes a RESTful web service. Definitions can include the following: URL -API paths that reflect object models, for example, user/43/comment/?datetime=01022013

-HTTP verbs that reflect the nature of the change, such as GET

should not write data, etc. -HTTP maps response codes to response status, for example, return 200 OK

only if there is no error.

JSONP is a tool that allows you to make a cross-domain request without facing CORS security issues in the browser. While JS frameworks can abstract you from reality, behind the scenes JSONP injects the <script>

DOM into the page. JSONP URL includes a request parameter by default callback=<<nameForCallback>>

. The answer is in javascript format and has the form:nameForCallback({ error: false, myData: "banana"});



JSONP is always used GET

as an HTTP verb. The API should probably always return 2XX

states 2XX

, even in the event of an error, as otherwise browsers will ignore the response payload (they won't be available to your javascript code).

These facts exclude many aspects of API design that can be considered part of a RESTful service. If you absolutely need to use JSONP (CORS can't be solved), you can still incorporate some useful (for readability / maintainability) RESTful qualities in your API design, such as route paths that reflect your data models, etc.

0


source







All Articles