0

I'm doing a project using javascript for client side and servlets for server side. I'm trying to implement a way to update client info real time. i.e when a client update some info in the web application, other clients will also see the update. I found that long polling is a good technique for this. This is the code I tried to get to work.

function poll() {
        setTimeout(function() {
        $.ajax({ 
            type: "GET",
            url: "server",
            contentType: "application/json", 
            data: {
                type: "update",
                card: "string"
            },
            success: function(data) {
                alert(data);
            },
            error: function(data) {
                alert('eroor');
            },
            dataType: "json", 
            complete: poll });
        }, 5000);
} 

I'm trying to send a request to the server every 5 seconds and get the response with new updates. But in all the skeleton codes I saw in the web, data: is not set. Without setting it, how would the sever know the type of request it received since there are other types of requests too. But when I set data: no requests are sent from the client. But without setting data: requests are sent to the server. Is it wrong to set data: ? Without it how would I let the servlet know the type of the request?

I understand that like mentioned in here long polling is not what I'm trying to do. But can anyone explain what I should do and where I'm doing wrong.

Community
  • 1
  • 1

2 Answers2

0

Since you make a GET request, the data values are appended to the URL as URL parameters. Your servlet must then use request.getParameter("type") and request.getParameter("card") to extract the information from the request.

If you think that no request is sent, first check your console for errors and look at the net communications panel in the developer tools of your browser.

wero
  • 32,544
  • 3
  • 59
  • 84
  • I figured out that no requests are sent by the console. When data: is not set, I can see the responses in the console –  Apr 21 '16 at 10:13
  • @user11 Do you see that your browser tries to send a request? E.g. in Firebugs Net Panel, see https://getfirebug.com/wiki/index.php/Net_Panel – wero Apr 21 '16 at 10:15
  • @user11 check this. https://api.jquery.com/jquery.get . You can send as a GET request along with the data, by using $.get – Madura Pradeep Apr 21 '16 at 10:21
  • @wero data: JSON.stringify("string") solved the problem. The server log indicates the requests it received. But I think I'm not using long polling here. –  Apr 21 '16 at 10:23
  • @user11 probably your server has a problem if it the data does not come in this form. But you still seem to think that no request is sent when you do as shown in your question. – wero Apr 21 '16 at 10:35
0

data:

issue is how you set the data. If you want to send json object, you have to stringify before you send it, like below.

$.ajax({
  url: url,
  type: "POST",
  data: JSON.stringify(data),
  contentType: "application/json",
  complete: callback
});

Without it how would I let the servlet know the type of the request?

What you mean by this? to know the contentType? If so, send contentType parameter as above.

I understand that like mentioned in here long polling is not what I'm trying to do. But can anyone explain what I should do and where I'm doing wrong.

Yes. This is not exactly a long polling. This is how you send a request to the server in every 5 seconds. Anyway server should support to long polling.

Madura Pradeep
  • 2,378
  • 1
  • 30
  • 34
  • data: JSON.stringify(data) solved the problem. But how do I implement long polling with servlets? –  Apr 21 '16 at 10:24
  • That's something you have to do with server. Check following. http://stackoverflow.com/questions/8081895/implementing-long-polling-in-an-asynchronous-fashion – Madura Pradeep Apr 21 '16 at 12:33