-1

I'm trying to send a json object using ajax to a servlet. The object is to be changed and sent back to the client. This is the code I used to send the json object from client to server. function sendJson(jsonObj)

{
    var parsed = JSON.parse(jsonObj);
    $.ajax({
        type: 'get', 
        url: 'GameLogic',
        dataType: 'JSON',
        data: { 
          loadProds: 1,
          parsed: JSON.stringify(parsed)
        },
        success: function(data) {

        },
        error: function(data) {
        alert('fail');
            }
    });
}

I only have a basic knowledge of javascript. As I understand this piece of code just sends a json object to a servlet. When receiving the response from the servlet, how do I get it? I searched for this and found functions similar to above function to receive response. I don't understand what this success: function(data) part does. Can someone explain me the way to send a json object and receive the response to and from a servlet. When I send a json object to the servlet, is there any way I can know whether it is received by the servlet, other than sending the object back as the response.

  • Does this answer all your questions wrt Servlets+Ajax? http://stackoverflow.com/q/4112686 Given the current way how you use $.ajax, you seem to lack some fundamental understanding. – BalusC Apr 12 '16 at 11:34

2 Answers2

1

Ver simply, the answer is already in your code. The ajax method of jquery has to callback methos for success and error. Both are already impl. in your example but doing nothing!!

Here your code with comments pointing to the callback impl.

{
    var parsed = JSON.parse(jsonObj);
    $.ajax({
        type: 'get', 
        url: 'GameLogic',
        dataType: 'JSON',
        data: { 
          loadProds: 1,
          parsed: JSON.stringify(parsed)
        },
        success: function(data) {
          // PROCESS your RESPONSE here!!! It is in "data"!!!!
        },
        error: function(data) {
            // This is called when the request failed, what happend is in the "data"!!!
        alert('fail');
            }
    });
}

Impl. something in the success callback and debug it with your browser dev tools to see what's inside of "data".

As you changed your question more about how to handle the communication in general and how to know if your request was received. Here my normal approach.

First I define an envenlope for every request and response which is always the same. It can look like this:

{
  status: OK | ERROR,
  message: "possible error message etc."
  data: JSON Object representing the payload.
}

After doing this I can impl. a generic logic to send and receive message between server and client and every side nows how to handle the envelope. To make sure a message is received, could be processed etc.

Then you have this:

  1. Make an ajax call to your server. 2a. If there is topoligical problem your error callback on client side is called. Request failed, server not reachable! 2b. The message was received by the server. The server can now process the payload regarding the URL used to call the server. The server method succeed it will write an OK in the envelop and his possible result in "data" as payload. If the method fails, it sets "status" to "ERROR" and provides an proper message, data is empty.
  2. The client receives data on the success callback and it can inteprete the "status" field if it's a usefull response or if it's an error.

Hope that helps

Rene M.
  • 2,660
  • 15
  • 24
0

The success:function() part goes like this

A function to be called if the request succeeds. The function gets passed three arguments:

  1. The data returned from the server, formatted according to the dataType parameter or the dataFilter callback function, if specified

  2. a string describing the status

  3. the jqXHR (jQuery-XHR) object

What this means is - if your ajax request was successful, the server will return you some response, ie, the data. This "data" can be used in the function.


$.ajax({
    ...
    success: function(data) {
        // process the "data" variable
        console.log("SERVER RESPONSE");
        console.log(data);
    }
});
Community
  • 1
  • 1
zhirzh
  • 3,273
  • 3
  • 25
  • 30