I've been tearing my hair out with this the last few days but I've received a large number of reports recently about users receiving an "undefined" response from .ajax
across multiple different .ajax
submission forms across the site.
Here is a sample of one of my submission forms:
//Post Request via Ajax
jQuery.ajax({
url: submissionurl,
type: "POST",
data: {
deckDescription : deckDescription,
deckExcerpt : deckExcerpt,
deckformat : deckformat,
youtubeLink : youtubeLink,
maindeckarray : JSON.stringify(maindeckarray),
maindeckarray_og : JSON.stringify(maindeckarrayog),
maindecknamesarray : JSON.stringify(maindeckNamearray),
extradeckarray : JSON.stringify(extradeckarray),
sidedeckarray : JSON.stringify(sidedeckarray),
submit_date : today,
coverCard : coverCard,
deckSourceName : deckSourceName,
deckSourceURL : deckSourceURL,
publicDeck : publicDeck,
decknumber : decknumber
},
success:function(response) {
jQuery('#notifyToastSubmit .toast-body').html('<span style="color:green">' response.success '</span>');
jQuery('#notifyToastSubmit').toast('show');
jQuery('form :input[type="submit"], form :input[type="text"], textarea').prop('disabled', true).css({"background-color" : "grey", "cursor" : "not-allowed"});
},
error: function(xhr, status, error) {
var err = JSON.parse(xhr.responseText);
jQuery('#notifyToast .toast-body').html(err.error);
jQuery('#notifyToast').toast('show');
jQuery('form :input[type="submit"]').prop('disabled', false);
}
});
My PHP script will then run through it and if successful return:
http_response_code(200);
header('Content-Type: application/json');
echo json_encode(array("success" => "Your deck has been submitted! You can view your submitted deck <a href='https://ygoprodeck.com/deck/".$prettyurl."' class='deckLink'>here</a>."));
exit();
Otherwise, in all other instances it will return some form of an error:
http_response_code(400);
header('Content-Type: application/json');
echo json_encode(array("error" => "You must have at least 40 cards in your Main Deck."));
exit();
From all of the screenshots my users have sent me, I can see that the success
function of the .ajax
call is firing because the "undefined" message they are showing is in green.
No error is reported from the PHP script. Nothing is going into the error_log
to signify an issue with the script. However, the data is never inserted when this occurs. It's almost as if the script isn't running at all at the time of submission. This issue involves multiple variations of PHP scripts with .ajax
calls across the site.
To be thorough, I've checked my server modsec logs and the Cloudflare firewall logs and none of the scripts are being blocked.
My most recent change has been to modify the .ajax
calls slightly to this but I'm awaiting further user responses to see if this has temporarily fixed the issue:
success:function(response) {
if(!response.success){
var returnmsg = "An error has occurred when submitting. Please try to hit the submit button again.";
}else{
var returnmsg = response.success;
jQuery('form :input[type="submit"], form :input[type="text"], textarea').prop('disabled', true).css({"background-color" : "grey", "cursor" : "not-allowed"});
}
jQuery('#notifyToastSubmit .toast-body').html('<span style="color:green">' returnmsg '</span>');
jQuery('#notifyToastSubmit').toast('show');
}
And finally, I had been testing this system for months and had never once seen this error. Unfortunately, it's never once shown up for me but I've received too many reports now from different users using different parts of the site to ignore it.
CodePudding user response:
I finally figured this out.
At the top of all of my PHP scripts, I was calling:
session_start();
However, we were making use of google workbox on the front end for caching.There's an occurence where the user PHP session has actually expired (due to being inactive) but the google workbox had cached the page and showed them logged in (a further refresh would have shown them logged out) so when the script is called, the session was empty.
My script was running this check:
if (!isset($_SESSION['loggedin'])) {
$hostname = getenv('HTTP_HOST');
header('Location: https://'.$hostname.'/login.php');
exit;
}
It only occurred to me last night that this makes no sense. The script redirects itself to the login.php
file which results in a HTTP code 200
which the ajax call is seeing and then firing the success function.
My website also makes use of a "remember me" function which my script wasn't utilizing. So, to "fix" my issue I have:
- Include the remember me function on my script calls so that the script itself can regenerate the session if they have checked remember me.
- Instead of redirecting to the login file, just fire back a message telling the user they need to log in.