Showing posts with label JavaScript. Show all posts
Showing posts with label JavaScript. Show all posts

Thursday, January 26, 2012

Why REST with JSONP when you can CORS?

JSONP (JSON with padding) is a hack used by JavaScript developers by wrapping a JSON (JavaScript Object Notation) document within a function call. So, if the JSON document looked like {"givenName":"John", "familyName":"Smith"}, the JSONP for the same would be callback({"givenName":"John", "familyName":"Smith"}) (callback being the commonly used wrapper function). So, if you are familiar with JSONP, you will realize that it is used to make Cross-Domain calls where JSON is not accepted by the browsers if they come from another domain. Thus, AJAX requests across a different domain was not possible through JSON and hence using JSONP was the common hack.

The problem with using JSONP is that it is called as a JavaScript function response and hence CORS-supportyou will not be able to use it as normal HTTP calls. That means you would not be able to send HTTP headers and that can be a problem at many places. There are hacks to deal with the problem, but these are best described as hacks. Another limitation of the JSONP hack was that you could only make GET requests and nothing more. Hence to make a standard, W3C created the CORS (Cross-Origin Resource Sharing) standard which nearly all modern browsers support.

The main issue with using JSON or AJAX (XHR – XMLHttpRequest) across domain was that browsers would not be able to acknowledge if the response was malicious or in response to the request that they made. The same-origin-policy, prevented making cross-domain requests. Then came CORS, a technique by browsers to check the origin policy first and then accept responses. So a server that wants to allow getting any type of HTTP request from another domain would list the domain or * in its HTTP response header as follows:

Access-Control-Allow-Origin: *
Access-Control-Allow-Origin: http://example.com:8080 http://foo.example.com

This means that there is some modification to be done on the server-side resource to add these headers to the response. Another point to note is that it works with XHR requests as well as client errors (4xx) and server errors (5xx).

Excellent examples on how to use CORS can be found at HTML5Rocks. On the server-side of things, you can find resources to enable CORS.

In Tomcat for a Java web application, you can enable CORS using the CORS Filter library. Basically you copy the jar file into Tomcat lib or WEB-INF/lib of your application and then add filters in your application’s web.xml or tomcat’s global conf/web.xml. For a resource which requires Basic Authentication and Cookies can be configured as follows:

<filter>
<filter-name>CORS</filter-name>
    <filter-class>com.thetransactioncompany.cors.CORSFilter</filter-class>
    <init-param>
     <param-name>cors.allowOrigin</param-name>
        <param-value>*</param-value>
    </init-param>
    <init-param>
     <param-name>cors.supportedMethods</param-name>
        <param-value>GET, POST, HEAD, PUT, DELETE</param-value>
    </init-param>
    <init-param>
     <param-name>cors.supportedHeaders</param-name>
        <param-value>Content-Type, Last-Modified</param-value>
    </init-param>
    <init-param>
        <param-name>cors.exposedHeaders</param-name>
        <param-value>Set-Cookie</param-value>
    </init-param>
    <init-param>
        <param-name>cors.supportsCredentials</param-name>
        <param-value>true</param-value>
    </init-param>
</filter>
<filter-mapping>
        <filter-name>CORS</filter-name>
        <servlet-name>MyServlet</servlet-name>
</filter-mapping>

Instructions for Cookies in Safari can be challenge. A nice post to workaround can be found here.

Wednesday, September 3, 2008

Google Chrome's Beats Others at JavaScript Performance

With Google Chrome released, one of its most awesome features is the excellent JavaScript performance. It is a complete new re-write of the JavaScript VM and uses what is called the V8 Engine by the Google developers who worked on the new JavaScript engine. Here is a JavaScript Performance Analysis for developers to look at comparing different browsers.

Google uses a lot of AJAX and the new V8 engine is the king in JavaScript performance done using Dromaeo :

Browser Time (lower the better)
Chrome 0.2.149.27 488.80ms
Firefox 3.0.1 1775.00ms
Opera 9.52 1767.60ms
Safari 3.1.2 2075.00ms

The memory usage of Chrome is also pretty good and the built-in task manager for looking at the memory/CPU usage of tabs is interesting and useful. It can also help in analyzing which tab is creating some kind of menace or using more memory. You can close that tab and get some free memory. This is just an initial preview, but the JavaScript performance is top-notch!!

Tuesday, July 15, 2008

Opera Users Can Now See Gmail 2 Default

Opera users have always been one of the outsiders to most web developers because of the low market share that the browser holds. Nevertheless, Opera has been implementing web standards quite quickly compared to other popular browsers. And so was the case with Gmail, where Opera users had to use the ”nocheckbrowser” parameter to see the newer Gmail2 interface. But that’s no more required, since Google has made changes and now Opera 9.51 and higher displays the new Gmail2 interface by default.

The newer Gmail2 interface is a little more AJAX-driven and has an initial loading screen after you login to your account. The interface also has a few nifty colored features that many Opera users were not able to see earlier. There was also a project on Google Code that was working towards making Gmail 2 compatible with Opera, just the way it works for other browsers like Firefox or Internet Explorer. The issue was with some incompatibilities between the JavaScript implementation in Firefox/Internet Explorer and Opera. Gmail developers were a little lazy to apply the hack earlier, but now they just finished their work.

So there you have it now... Enjoy Gmail 2 on Opera without any quirks or problems!