0

I am trying to retrieve from a servlet using Jquery

$(document).ready(function() {
    $("#mybutton").click(function(event) {
        alert('1');
        $.get("http://localhost:8888/ntlm/getuser", function(data) {
            alert('data ' + data);
            $(".errorMssg").text("Data Loaded: " + data);
            alert('data ' + data);
        });
        return true;
    });
});

and in GetUser servlet I have

public void doGet(HttpServletRequest request, 
                  HttpServletResponse response) throws ServletException, 
                                                       IOException {
    response.setContentType(CONTENT_TYPE);
            response.setContentType("text/html");
    PrintWriter out = response.getWriter();
    out.print("Authenticating?");
}

protected void doPost(HttpServletRequest request, 
                      HttpServletResponse response) throws ServletException, 
                                                           IOException {
    doGet(request, response);
}

If I directly access http://localhost:8888/ntlm/getuser, I am able to see output, however when I calling using Jquery, not able to see the output.

What could be the reason for this?

Esailija
  • 138,174
  • 23
  • 272
  • 326
Jacob
  • 14,463
  • 65
  • 207
  • 320
  • 1
    What does the console say? – Alkis Kalogeris Apr 12 '13 at 07:06
  • @alkis Response is empty. – Jacob Apr 12 '13 at 07:08
  • What does *empty* mean? You see `alert('data')` twice? – Paul Grime Apr 12 '13 at 07:21
  • @PaulGrime I do not see `alert('data')` – Jacob Apr 12 '13 at 07:24
  • Is the web page also on `http://localhost:8888`? Otherwise the browser will stop a cross-domain request (without special exception). Is the request sent? Does the server receive the request? – Paul Grime Apr 12 '13 at 07:27
  • @PaulGrime I am calling the servlet which is not in the same context. It is deployed as another application – Jacob Apr 12 '13 at 07:30
  • And the other questions? – Paul Grime Apr 12 '13 at 07:32
  • @PaulGrime Request has been sent, however it doesn't looks like it received the request. – Jacob Apr 12 '13 at 07:38
  • Can you confirm there are no JS errors in the console and that the Ajax request is leaving the browser as expected - correct url etc? And that when you copy and paste that URL into another browser window that it works? – Paul Grime Apr 12 '13 at 07:52
  • Dont use the whole path.. Just use the `` that you have mentioned in your `web.xml` or in the annotation. – Meherzad Apr 12 '13 at 07:59
  • @PaulGrime There are no JS errors and when I am directly accessing my servlet I am able to see the output. – Jacob Apr 12 '13 at 08:00
  • @PaulGrime I can see the following in chrome console though `XMLHttpRequest cannot load http://localhost:8888/ntlm/authenticate. Origin http://192.168.1.2:8988 is not allowed by Access-Control-Allow-Origin` – Jacob Apr 12 '13 at 08:08
  • I asked if the web page was on the same protocol, host and port as the Ajax request and you said yes, but that isn't the case. This is a cross domain issue. – Paul Grime Apr 12 '13 at 08:26
  • @PaulGrime How to deal with this? Any workaround? – Jacob Apr 12 '13 at 08:30
  • If 192.168.1.2 is an alias for localhost (i.e. it's your machine's IP) then strip the protocol, host and port from the Ajax and just use `/ntlm/getuser`. If the `/ntlm/getuser` resource is genuinely on a different server than your web pages, then you have to use CORS, or JSONP, or change your design. – Paul Grime Apr 12 '13 at 08:32
  • @PaulGrime How I can use JSONP to resolve my problem? – Jacob Apr 12 '13 at 08:50
  • http://stackoverflow.com/questions/2681466/jsonp-with-jquery – Paul Grime Apr 12 '13 at 09:16
  • @PaulGrime Thanks for this, in my case what will be the callback? – Jacob Apr 12 '13 at 09:34
  • Nothing. The `callback=?` part of the requested URL is a signal to jQuery to use JSONP, and provided your server response is in a JSONP compatible format, then your callback will be called. Read the linked answer and the jQuery docs carefully. – Paul Grime Apr 12 '13 at 09:46
  • @PaulGrime Thanks a lot for pointing out the problem and suggesting the correct example, I have resolved the issue. If you could post an answer I will be glad to accept it. – Jacob Apr 12 '13 at 15:44

1 Answers1

1

To perform cross-domain requests, e.g. a request to http://localhost:8888/ntlm/authenticate from a web page served from http://192.168.1.2:8988, you will either need to enable CORS or use JSONP.

jQuery JSONP.

Community
  • 1
  • 1
Paul Grime
  • 14,970
  • 4
  • 36
  • 58