David's Blog

Web development and programming

Google Playground

Today I would like to talk about an amazing tool developed by Google that is not too popular among web developers: Google Playground.

Google Code Playground

Google Code Playground is very powerful tool provided by Google in order to help developers “playing” with its APIs such as Language, Maps, Visualization, etc. You can navigate between a wide quantity of examples for each API, each example comes with the source code for implementing them.

Google Code Playground

Google Code Playground

But actually Google Playground is more than this.  Continue reading

JSONP with jQuery

Today I would like to introduce you a faster and easy way for working with JSONP thanks to the jQuery JavaScript library.

To demostrate how to use JSONP with jQuery let’s take the same REST service of my latest post about JSONP, the Google Geocoding API (V2).

jQuery has a function called ajax() which allows developers to invoke AJAX for calls between same domain and JSONP for calls between cross domains.

At least four options have to be set as argument of jQuery ajax function:

  • url: the URL of the service. This argument is going to be filled dynamically to take the place from the input field of the form. (See more details in the post  JSONP: JSON with padding complex example)
  • dataType: for JSONP calls must always be ‘jsonp’, we are handling JSONP format.
  • crossDomain: for JSONP calls must always be ‘true’ if we are making a call to a different domain. (We also can make JSONP calls in the same domain, but in this case is much more simple to use AJAX and JSON as return format).
  • success: the function where the JSON passed as argument of the JSONP is processed. In it should be implemented the logic for processing the JSON.

Continue reading

JSONP: JSON with Padding

Introduction

JSONP (JSON with padding) is a method for making asynchronous requests in different domains, it is commonly used for invoking RESTful Services. JSONP is conceived because of the lack of AJAX for performing requests to resources that are in different domain. The same origin policy prevents that AJAX from being used across domain. A web page which is placed under davidsblog.eu can only access (through AJAX) to data which is under the domain davidsblog.eu and in the same port (in this case the port 80).

JSON

For understanding JSONP, the first requirement is to understand JSON. JSON (JavaScript Object Notation) is a lightweight data interchange format. This format is copied from the way for representing associative arrays (objects) in JavaScript.

This is a JSON which represents a person with three properties: name, age and sex.

{
    "name" : "John"
    "age" : 23,
    "sex": "MALE"
}

In JavaScript this type of structures can be accessed in two ways:

  • Associative array style:
var name = json["name"];
  • Through point operator:
var name = json.name;

Once JSON is understood, JSONP format is just “function(json)”, a function whose unique parameter is a JSON. Example:

processPerson({ "name" : "John", "age" : 23, "sex": "MALE" });

Yes, it is not too complicated at all.

How can we do a JSONP call?

We have to include the URL of the service in a <script> HTML tag:

Continue reading