1

I have this $.post peace of code:

$.post("../admin-login",
{
   dataName:JSON.stringify({
   username:uname,
   password:pass,
   })

}, function(data,status){
    console.log("Data:"+data);
    answer = data;
    }
);

and I wont to transform it to $.ajax. On the servlet side I am demanding request.getParamter("dataName") but I do not know how to write data: section in $.ajax so that I can get parameters like that(request.getParamter("dataName"))? Also, it seems to be problem with this type of code, I am asuming cause of async, that I cannot do this:

var answer="";

function(data,status){
        console.log("Data:"+data);
        answer = data;
}

And that answer is keeping empty(""), even though in console is written in deed "true" or "false" as my server answers. What is this about? Thanks in advance.

I found out that problem is in the click() event. Ajax finishes when click() finishes, so I am not able to get data before event is done. What is bad in that is that I cannot fetch data because it is finished. Does anyone know how to solve this?

executos
  • 15
  • 5

3 Answers3

1
$.post("../admin-login",
{
   dataName:JSON.stringify({
   username:uname,
   password:pass,
   })

}, function(data,status){
    console.log("Data:"+data);
    answer = data;
    }
);

becomes

function getResult(data) {
  // do something with data
  // you can result = data here
  return data;
}

$.ajax({
  url: "../admin-login",
  type: 'post',
  contentType: "application/x-www-form-urlencoded",
  data: {
   dataName:JSON.stringify({
   username:uname,
   password:pass,
   })    
},
  success: function (data, status) {
    getResult(data);
    console.log(data);
    console.log(status);
  },
  error: function (xhr, desc, err) {
    console.log(xhr);
  }
});
jsam
  • 301
  • 1
  • 7
  • Do you know why I cannot write: var result=""; success: function (data, status) { console.log(data); console.log(status); result = data; } ? Thanks. – executos Jun 23 '15 at 11:49
  • If you want to do that you need to do `var result="";` before the `$.ajax` call and then inside the success function you can do `result = data`. But be carefull because that is async. – jsam Jun 23 '15 at 11:51
  • Yeah, that async is making me headaces. I tried using async:false, but it still won't wait for server response and result is still empty. – executos Jun 23 '15 at 11:55
  • You can also take a look at Promises since they will sove your problem. This is a great response http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-asynchronous-call – jsam Jun 23 '15 at 11:58
  • I found out that problem is in the click() event. Ajax finishes when click() finishes, so I am not able to get data before event is done. What is bad in that is that I cannot fetch data because it is finished. Do you know how to prevent this kind of behavior? Thanks. – executos Jun 23 '15 at 12:26
0

You need to see how the information os arriving to your servlet as query parameter or payload.

See this HttpServletRequest get JSON POST data

Community
  • 1
  • 1
arterzatij
  • 82
  • 7
0

You could try structuring your AJAX request like the below:

var dataName = username:uname, password:pass;

$.ajax({
    url: "../admin-login",
    data: JSON.stringify(dataName),
    type: "POST",
    cache: false,
    dataType: "json"
}).done(function(data, status) {
    console.log("Data:"+data);
    answer = data;
});
NightOwlPrgmr
  • 1,322
  • 3
  • 21
  • 31