I want, without pressing a button, to receive a response from the server, that is, to receive a response and use it for the condition. Is it possible?

Button -

<button class="btn btn-success" id="confirmButton" data-tid="0">Button</button> 

Onclick assigned to the button, sort of like this way -

  $("#botFilter .btn").removeClass("active"); $(this).addClass("active"); doFilter(); }); $("#confirmButton").on("click",function(){ inlineAlert("","Confirming trade offer - please wait..."); $this=$(this); $this.prop("disabled",true); var tid=$this.data("tid"); $.ajax({ url:"scripts/_confirm.php", type:"GET", data:{"tid":tid}, success:function(data){ try{ data=JSON.parse(data); if(data.success){ if(data.action=="accept"){ inlineAlert("success",data.result); }else{ inlineAlert("cross",data.result); } 

In this case, I myself understand bad, please explain in more detail.

    1 answer 1

    Here is the code section you are interested in:

     $this=$(#confirmButton); // селектим кнопку с id = confirmButton var tid=$this.data("tid"); // берем значение параметра data-tid из нее // отправляем запрос на сервер $.ajax( { url:"scripts/_confirm.php", type:"GET", data:{"tid":tid}, success:function(data){ // здесь обрабатываем успешный ответ от сервера. // т.е. именно в этом месте Вы должны что-либо // делать с результатом ответа. // сам результат находится в переменной data console.log(data); try{ var data = JSON.parse(data); if(data.success){ if(data.action=="accept"){ inlineAlert("success",data.result); }else{ inlineAlert("cross",data.result); } } }finally{ }; } } ); 
    • I've tried to do the same kind of request, or I didn't find everything I needed in the code, when I send a request for it in the console, I write the answer SyntaxError: illegal character - SloGS
    • And although there is no already figured out, for some reason, when you click on a button, the request is sent to the file scripts/_confirm.php?tid=1253042471 , and when you send a request, the code is sent just to scripts/_confirm.php - SloGS
    • SyntaxError: illegal character is a syntax error. somewhere a symbol is lost. This is a good google. try the original code otdebazhit in the console (example for chrome - learn.javascript.ru/debugging-chrome ) - bmsdave
    • Thank you all very much, figured out, they helped a lot !!!!!! - SloGS