Repeating AJAX request after login

I am sending some information via AJAX to PHP-Script to get the text to be displayed. There are no problems so far. But if the user logs out, the result will be false and a modal with a login form will be shown.

If the user is logged in, the first information ( var data ) must be sent again, since the first sending was not accepted.

 $.ajax({ url: "script.php", type: "POST", data: data, dataType: "json" }) .done(function( json ) { if (json.result === false) { showModal("login"); return; } else { $('#result').html(json.result); } }); 

The showModal function is also associated with the ajax request, so the user logs in ... After that, the first data should be sent again ...

 function showModal() { $('body').append('<form>...'); // Show Modal with form to login } // With submit form, the user will be logged in $('body').on('submit','#loginform',function(event){ $.ajax({ url: "login.php", type: "POST", data: { 'username': username, 'password': password }, dataType: "json" }) .done(function( json ) { // User is now logged in // Now repeat first request }); }); 
+6
source share
2 answers

Put your code inside the function. You can call a function when you need it:

 var sendData = function() { $.ajax({ url: "script.php", type: "POST", data: data, dataType: "json" }) .done(function( json ) { if (json.result === false) { showModal("login"); return; } else { $('#result').html(json.result); } }); }; // now run sendData() when you want to trigger it 

When to call sendData() , the second time depends on how your login (showModal) works. Find a way to catch the "successful login" event.


You can pass sendData to the showModal function and call it there. Therefore, showModal does not need to know anything about data :

 var sendData = function() { $.ajax({ url: "script.php", type: "POST", data: data, dataType: "json" }) .done(function( json ) { if (json.result === false) { // The function sendData is passed as a parameter - will be called after a successful login showModal("login", sendData); return; } else { $('#result').html(json.result); } }); }; 

Then, where showModal is defined:

 function showModal(dialog, loginCallback) { $('body').append('<form>...'); // Show Modal with form to login // With submit form, the user will be logged in $('#loginform').on('submit', function(event) { $.ajax({ url: "login.php", type: "POST", data: { 'username': username, 'password': password }, dataType: "json" }) .done(function( json ) { // User is now logged in // Now repeat first request loginCallback(); }); }); } 
+4
source

You can have a ShowModal function to accept another argument as ajax parameters. If ajax parameters are defined, just name them done.

 function showLoginModal(ajaxOptions){ $.ajax({ url: "login.php", type: "POST", data: { 'username': username, 'password': password }, dataType: "json" }) .done(function( json ) { if(ajaxOptions!== undefined){ $.ajax(ajaxOptions); } }); } 

Then pass ajaxOptions from your calling function

 $.ajax({ url: "script.php", type: "POST", data: data, dataType: "json" }) .done(function( json ) { if (json.result === false) { showLoginModal(this); } else { $('#result').html(json.result); } }); 
+1
source

Source: https://habr.com/ru/post/985836/


All Articles