Cross Domain Ajax Call


Web security policy restrict the cross-domain XMLHttpRequest (Ajax) request, for better, safer mash-up within the web application. There is work around to make Ajax request across domain.

I have created sample server-client application to demonstrate this using jquery.

Server side program which send server information in JSON format (JSON is must) .

Server Side JSP (test.jsp) on server [ cdacmumbai.in ]


<%@ page import="java.util.Date" %>
<%@page import="java.net.InetAddress;" %>
<%
        Date date = new Date();
        InetAddress inetAddress = InetAddress.getLocalHost();

        response.setContentType("application/x-javascript; charset=UTF-8");

        String data = "{\"hostname\":\""+inetAddress.getHostName()+"\", \"serverip\":\""+inetAddress.getHostAddress()+"\", \"servertime\":\""+date.toString()+"\"}";
        String foutput = request.getParameter("callback")+ "("+data+");";
        out.println( foutput.trim() );  
        out.close();
%>

Client code (test.html) on Server[ cdac.in ]

<html>
        <head>
                <script type="text/javascript" src="./jquery-latest.js"></script>
                <script>
                function fun(){
                        $.ajax({
                                url : "http://cdacmumbai.in/test.jsp?out=json&callback=?",
                                dataType: "json",
                                contentType: "application/json;charset=utf-8",
                                type: "GET",
                                success: function ( output ) {
                                        var data = eval( output );
                                        document.getElementById("datetime").innerHTML = "Server Date&Time: "+data.servertime;
                                        document.getElementById("hostname").innerHTML = "Server Hostname: "+data.hostname;
                                        document.getElementById("serverip").innerHTML = "Server IP Address: "+data.serverip;
                                }
                        });

                }
                </script>
        </head>
        <body>
                <input type="submit" value="getServerInformation()" onclick="return fun()">
                <br>
                <br>
                <div>
                        <div id="datetime">
                        </div>
                        <br>
                        <div id="hostname" >
                        </div>
                        <br>
                        <div id="serverip">
                        </div>
                </div>
        </body>
</html>
Advertisements

2 thoughts on “Cross Domain Ajax Call

  1. Pingback: AJAX | manishugave

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: