Jquery Failing In Electron

Hello,

I’m doing an ajax call in Electron that only fails sometimes. If I copy and paste the data into the php server file, it works 100% of time. Sadly, this is killing my workflow, and it happens on a few important ajax calls. The error says invalid json, invalid token ‘<’.

Jquery - 3.6.1
Electron - 22.0.0

Ajax:

 $.ajax({
        url: 'http://localhost/OtherRealms/php/scripts/update_character_xml_field.php',
        dataType: 'json',
        type: 'POST',
        contentType: 'application/json; charset=utf-8',
        data: pipSkillData,
        success: function( data, textStatus, jQxhr ){
                                  //console.log(data);
          },
       error: function( jqXhr, textStatus, errorThrown ){
                       console.log( errorThrown );
        }
              // End updata skill
         });
               // end fetch skill tier
                 },
        error: function( jqXhr, textStatus, errorThrown ){
                   console.log( errorThrown );
              }
         });

The server code shouldn’t be needed. I’ll just share what I’ve placed at the top for data.

$data[‘characterToken’] = ‘2205d49f4165e46eb3b65e32a1fa02d3’;
$data[‘pipType’] = ‘pip skill’;
$data[‘skillName’] = “magic”;
$data[‘skillType’] = “skill”;
$data[‘tier’] = “II”;
$data[‘userSerial’] = “9e0b6bf61339998735ff478db747ba04”;
$data[‘userToken’] =“58e4d9a44dc47b84d4a2db84e9188cdc” ;


Here's a couple of images
https://ibb.co/kMmmq7S
https://ibb.co/ZSmCMd8

If this can't be solved is there a work around? Would vanilla Ajax calls be better?

That’s probably the start of a php error from the server-side code. What does the full response show in the browser’s developer tools network tab?

Wow, you just taught me some important debugging technique. There is an error on the script. On line 50. My xml file is opening as bool. But I don’t know why in that instance. Because it works several other times…

Line 50 is this line:

$charactersDoc = simplexml_load_file($charactersXML);

But the entire script is pretty straight forward…

<?php
// Implement: Finish Security
header('Content-Type: application/json; charset=utf-8');

$rawData = file_get_contents("php://input");
$data = array();
parse_str($rawData, $data);
$returnMessage = array();

/*$data['characterToken'] = '2205d49f4165e46eb3b65e32a1fa02d3';
$data['pipType'] = 'pip skill';
$data['skillName'] = "magic";
$data['skillType'] = "skill";
$data['tier'] = "II";
$data['userSerial'] = "9e0b6bf61339998735ff478db747ba04";
$data['userToken'] ="58e4d9a44dc47b84d4a2db84e9188cdc" ;*/

if(!isset($_SESSION['ID']) && !isset($_SESSION['serial']))
	{
	
		$charactersXML = __DIR__ . '../../../../../Yen7AaH9rb/database/characters.xml';
		$usersXML = __DIR__ . '../../../../../Yen7AaH9rb/database/users.xml';
		
		if(isset($data['userToken'])
		   && isset($data['userSerial'])
		   && isset($data['characterToken'])
		   && isset($data['pipType']))
			{

				// test if user and serial match
				
				if(is_file($usersXML))
				{
					
					$userToken = $data['userToken'];
					$characterToken = $data['characterToken'];
					
					$usersDoc = simplexml_load_file($usersXML);
					$user = $usersDoc->xpath("//users/user[@token='$userToken']");
										
					if(!empty($user))
					{
					    
						$userSerial = $user[0]->serial['value'][0]->__toString();
						
						if($data['userSerial'] === $userSerial)
						{
							
							// check if the character exists
							$charactersDoc = simplexml_load_file($charactersXML);
							$characterDoc = new SimpleXMLElement($charactersDoc->asXML());
							
							$character = $charactersDoc->xpath("//character[@token='$characterToken']");
						
							if(!empty($character))
							{
								
								switch($data['pipType'])
								{
									
									case 'pip amount up':
										// pip field up by amount
										switch($data['field'])
										{
										
											case 'hitPoints':
												$characterDoc->xpath("//character[@token='$characterToken']")[0]->hitPoints['value'] = $characterDoc->xpath("//character[@token='$characterToken']")[0]->hitPoints['value'] + $data['value'];
											break;
											case 'spiritPoints':
												$characterDoc->xpath("//character[@token='$characterToken']")[0]->spiritPoints['value'] = $characterDoc->xpath("//character[@token='$characterToken']")[0]->spiritPoints['value'] + $data['value'];
											break;
											case 'characterPoints':
												$characterDoc->xpath("//character[@token='$characterToken']")[0]->characterPoints['value'] = $characterDoc->xpath("//character[@token='$characterToken']")[0]->characterPoints['value'] + $data['value'];
											break;
											case 'skillPoints':
												$characterDoc->xpath("//character[@token='$characterToken']")[0]->skillPoints['value'] = $characterDoc->xpath("//character[@token='$characterToken']")[0]->skillPoints['value'] + $data['value'];
											break;
											case 'craftingLicenses':
												$characterDoc->xpath("//character[@token='$characterToken']")[0]->craftingLicenses['value'] = $characterDoc->xpath("//character[@token='$characterToken']")[0]->craftingLicenses['value'] + $data['value'];
											break;
											case 'gambit':
												$characterDoc->xpath("//character[@token='$characterToken']")[0]->gambit['value'] = $characterDoc->xpath("//character[@token='$characterToken']")[0]->gambit['value'] + $data['value'];
											break;
											case 'labourPoints':
												$characterDoc->xpath("//character[@token='$characterToken']")[0]->labourPoints['value'] = $characterDoc->xpath("//character[@token='$characterToken']")[0]->labourPoints['value'] + $data['value'];
											break;
											case 'toxLimit':
												$characterDoc->xpath("//character[@token='$characterToken']")[0]->toxLimit['value'] = $characterDoc->xpath("//character[@token='$characterToken']")[0]->toxLimit['value'] + $data['value'];
											break;
											case 'toxLimitMax':
												$characterDoc->xpath("//character[@token='$characterToken']")[0]->toxLimitMax['value'] = $characterDoc->xpath("//character[@token='$characterToken']")[0]->toxLimitMax['value'] + $data['value'];
											break;
											case 'valour':
												$characterDoc->xpath("//character[@token='$characterToken']")[0]->valour['value'] = $characterDoc->xpath("//character[@token='$characterToken']")[0]->valour['value'] + $data['value'];
											break;
											case 'soul':
												$characterDoc->xpath("//character[@token='$characterToken']")[0]->soul['value'] = $characterDoc->xpath("//character[@token='$characterToken']")[0]->soul['value'] + $data['value'];
											break;
											case 'sleight':
												$characterDoc->xpath("//character[@token='$characterToken']")[0]->sleight['value'] = $characterDoc->xpath("//character[@token='$characterToken']")[0]->sleight['value'] + $data['value'];
											break;
											case 'beauty':
												$characterDoc->xpath("//character[@token='$characterToken']")[0]->beauty['value'] = $characterDoc->xpath("//character[@token='$characterToken']")[0]->beauty['value'] + $data['value'];
											break;
											case 'wisdom':
												$characterDoc->xpath("//character[@token='$characterToken']")[0]->wisdom['value'] = $characterDoc->xpath("//character[@token='$characterToken']")[0]->wisdom['value'] + $data['value'];
											break;
											case 'constitution':
												$characterDoc->xpath("//character[@token='$characterToken']")[0]->constitution['value'] = $characterDoc->xpath("//character[@token='$characterToken']")[0]->constitution['value'] + $data['value'];
											break;
											case 'aptitude':
												$characterDoc->xpath("//character[@token='$characterToken']")[0]->aptitude['value'] = $characterDoc->xpath("//character[@token='$characterToken']")[0]->aptitude['value'] + $data['value'];
											break;
											case 'courage':
												$characterDoc->xpath("//character[@token='$characterToken']")[0]->courage['value'] = $characterDoc->xpath("//character[@token='$characterToken']")[0]->courage['value'] + $data['value'];
											break;
											case 'perception':
												$characterDoc->xpath("//character[@token='$characterToken']")[0]->perception['value'] = $characterDoc->xpath("//character[@token='$characterToken']")[0]->perception['value'] + $data['value'];
											break;
											case 'fortitude':
												$characterDoc->xpath("//character[@token='$characterToken']")[0]->fortitude['value'] = $characterDoc->xpath("//character[@token='$characterToken']")[0]->fortitude['value'] + $data['value'];
											break;
											case 'reflexes':
												$characterDoc->xpath("//character[@token='$characterToken']")[0]->reflexes['value'] = $characterDoc->xpath("//character[@token='$characterToken']")[0]->reflexes['value'] + $data['value'];
											break;
											case 'willpower':
												$characterDoc->xpath("//character[@token='$characterToken']")[0]->willpower['value'] = $characterDoc->xpath("//character[@token='$characterToken']")[0]->willpower['value'] + $data['value'];
											break;
										}
									break;
									case 'pip amount down':
										switch($data['field'])
										{
											
											case 'hitPoints':
												$characterDoc->xpath("//character[@token='$characterToken']")[0]->hitPoints['value'] = $characterDoc->xpath("//character[@token='$characterToken']")[0]->hitPoints['value'] - $data['value'];
											break;
											case 'spiritPoints':
												$characterDoc->xpath("//character[@token='$characterToken']")[0]->spiritPoints['value'] = $characterDoc->xpath("//character[@token='$characterToken']")[0]->spiritPoints['value'] - $data['value'];
											break;
											case 'characterPoints':
												$characterDoc->xpath("//character[@token='$characterToken']")[0]->characterPoints['value'] = $characterDoc->xpath("//character[@token='$characterToken']")[0]->characterPoints['value'] - $data['value'];
											break;
											case 'skillPoints':
												$characterDoc->xpath("//character[@token='$characterToken']")[0]->skillPoints['value'] = $characterDoc->xpath("//character[@token='$characterToken']")[0]->skillPoints['value'] - $data['value'];
											break;
											case 'craftingLicenses':
												$characterDoc->xpath("//character[@token='$characterToken']")[0]->craftingLicenses['value'] = $characterDoc->xpath("//character[@token='$characterToken']")[0]->craftingLicenses['value'] - $data['value'];
											break;
											case 'gambit':
												$characterDoc->xpath("//character[@token='$characterToken']")[0]->gambit['value'] = $characterDoc->xpath("//character[@token='$characterToken']")[0]->gambit['value'] - $data['value'];
											break;
											case 'labourPoints':
												$characterDoc->xpath("//character[@token='$characterToken']")[0]->labourPoints['value'] = $characterDoc->xpath("//character[@token='$characterToken']")[0]->labourPoints['value'] - $data['value'];
											break;
											case 'toxLimit':
												$characterDoc->xpath("//character[@token='$characterToken']")[0]->toxLimit['value'] = $characterDoc->xpath("//character[@token='$characterToken']")[0]->toxLimit['value'] - $data['value'];
											break;
											
											
										}
									break;
									case 'set value':
										switch($data['field'])
										{
											
											case 'hitPoints':
												$characterDoc->xpath("//character[@token='$characterToken']")[0]->hitPoints['value'] = $characterDoc->xpath("//character[@token='$characterToken']")[0]->hitPoints['value'] = $data['value'];
											break;
											case 'spiritPoints':
												$characterDoc->xpath("//character[@token='$characterToken']")[0]->spiritPoints['value'] = $characterDoc->xpath("//character[@token='$characterToken']")[0]->spiritPoints['value'] = $data['value'];
											break;
											case 'toxLimit':
												$characterDoc->xpath("//character[@token='$characterToken']")[0]->toxLimit['value'] = $characterDoc->xpath("//character[@token='$characterToken']")[0]->toxLimit['value'] = $data['value'];
											break;
											case 'toxLimitMax':
												$characterDoc->xpath("//character[@token='$characterToken']")[0]->toxLimitMax['value'] = $characterDoc->xpath("//character[@token='$characterToken']")[0]->toxLimitMax['value'] = $data['value'];
											break;
											case 'description':
												// Implement: test this update
												$characterDoc->xpath("//character[@token='$characterToken']")[0]->description = $data['value'];
											break;
											
										}
									break;
									case 'pip skill':
										 switch($data['skillType'])
										 {
											 
											 case 'skill':
													foreach($characterDoc->xpath("//character[@token='$characterToken']")[0]->skills->skill as $skillIndex => $skill)
													{
																
														if($skill['name']->__toString() == $data['skillName'])
														{
																	
															$skill['tier'] = $data['tier'];
															
																	
														}
																
													}
											 break;
											 case 'craft':
													foreach($characterDoc->xpath("//character[@token='$characterToken']")[0]->crafts->craft as $skillIndex => $skill)
													{
																
														if($skill['name']->__toString() == $data['skillName'])
														{
																	
															$skill['tier'] = $data['tier'];
																	
														}
																
													}
											 break;
											
										 }
									break;
									
								}
								
								// write the xml back into the file beautified
								
								$dom = new DOMDocument;
								$dom->preserveWhiteSpace = FALSE;
								$dom->loadXML($characterDoc->asXML());
								$dom->formatOutput = TRUE;
								
								if(is_file($charactersXML))
								{
								
									file_put_contents($charactersXML, $dom->saveXML());
									$returnMessage['message'] = 'success';
									echo json_encode($returnMessage);
									
								}

							}else{
								
								// empty user data and go back to login screen
								
							}
							
						}else{
							
							// empty user data and go back to login screen
							
						}
				
					}
				}
				

			}else{
				
				
				// do nothing
				
			}
			
	}else{
		
		header('Location: ../../../index.php');
		exit;
		
	}
?>

So here is the error:

Blockquote

Warning: simplexml_load_file(): file:/C:/xampp/htdocs/OtherRealms/php/scripts…/…/…/…/…/Yen7AaH9rb/database/characters.xml:1: parser error : Document is empty in C:\xampp\htdocs\OtherRealms\php\scripts\advance_balance.php on line 57



Warning: simplexml_load_file(): in C:\xampp\htdocs\OtherRealms\php\scripts\advance_balance.php on line 57



Warning: simplexml_load_file(): ^ in C:\xampp\htdocs\OtherRealms\php\scripts\advance_balance.php on line 57



Fatal error: Uncaught Error: Call to a member function asXML() on bool in C:\xampp\htdocs\OtherRealms\php\scripts\advance_balance.php:58
Stack trace:
#0 {main}
thrown in C:\xampp\htdocs\OtherRealms\php\scripts\advance_balance.php on line 58

This is strange. So simple xml is saying the document is empty sometimes, and it seems only in Jquery ajax calls from Electron. It’s not every time either. Just when it’s convenient to mess with me, lol. I can’t seem to emulate an error just landing on the script file.