Hello, please tell me, never worked with ajax js tried to bypass it. Now we met face to face. It was necessary to get a variable from js in php. I decided on QS to make a request to another php and get a result.

$.ajax ({ url: url, data: { map : coords[0], map2 : coords[1]}, success: function( result ) { alert( result ); } }); 

Actually, I did, and then what? Variables remain in js because they get out in alert. How not to be told, something Google displays a lot of information, but not the one you need. I don’t know how to deal with this. The task is to update without action on the page. In essence, everything is sent and returned. But in fact, the variable still remains in js.

  • @Alex, the alert does not show the variable, but the result that you get from the server. Do not want to receive it - do not withdraw - Denis Heavenly

1 answer 1

 function somethingFunction(){ var variable_1 = "STRING"; var variable_2 = 123; var variable_3 = {json: 'format'}; // Формирование json для отправки var sendData = { variable_1: variable_1 , variable_2: variable_2, variable_3: variable_3}; $.ajax ({ type: 'POST', // Метод отправляемых данных (GET/POST) url: "/something", // путь до PHP Обработчика data: sendData, // Данные для отправки dataType: 'json', // Данные которые прийдут от обработчика, принимаемые значения (xml, json, script, or html) success: function(data) // Действие при успешной отправки { // Результирующие данные (data) после отправки на php обработчик, то есть что он выдаст после того как обработает данные. В формате JSON console.log(data); }, error: function(err) // Действие при неудачной отправки { console.error('Ошибка отправки данных'); //console.error('Статус ошибки: ', JSON.parse(err.responseText).status); //console.error('Название ошибки: ', JSON.parse(err.responseText).error); // 404 - страница не найдена. } }); } somethingFunction(); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>