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 [ ]

<%@ page import="java.util.Date" %>
<%@page import=";" %>
        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() );  

Client code (test.html) on Server[ ]

                <script type="text/javascript" src="./jquery-latest.js"></script>
                function fun(){
                                url : "",
                                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;

                <input type="submit" value="getServerInformation()" onclick="return fun()">
                        <div id="datetime">
                        <div id="hostname" >
                        <div id="serverip">

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: Logo

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

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s

Create a free website or blog at

%d bloggers like this: