Просмотр исходного кода

Fix bug where error message was not shown after submit

Alan Lam 8 лет назад
Родитель
Сommit
d6b2a1084b
2 измененных файлов с 15 добавлено и 4 удалено
  1. 12 2
      controllers/test.php
  2. 3 2
      public/js/app-test.js

+ 12 - 2
controllers/test.php

@@ -193,10 +193,20 @@ if(Core::$user->level == User::LEVEL_TESTER) {
 
 		}
 		catch(FormException $e) {
-			$e->generateAlerts();
+			if(HTTP::isAJAX()) {
+				$e->generateJSONError();
+			}
+			else {
+				$e->generateAlerts();
+			}
 		}
 		catch(Exception $e) {
-			Core::addAlert("danger", $e->getMessage());
+			if(HTTP::isAJAX()) {
+				JSON::fail($e->getMessage());
+			}
+			else {
+				Core::addAlert("danger", $e->getMessage());
+			}
 		}
 
 		date_default_timezone_set($previous_timezone);

+ 3 - 2
public/js/app-test.js

@@ -44,7 +44,8 @@ function initialiseTestForm() {
 
 				try {
 					var data = $.parseJSON(data);
-					if(!data.success) throw "Unsuccessful.";
+					if(!data.success && data.message) throw data.message;
+					else if(!data.success) throw "Please try again later.";
 
 					$submitStatus.html("Submitted. Thank you for participating in this test. You will be redirected to the Tests page in three seconds.");
 					setTimeout(function() {
@@ -52,7 +53,7 @@ function initialiseTestForm() {
 					}, 3000);
 				}
 				catch(err) {
-					$submitStatus.html("Error, please try again later.");
+					$submitStatus.html("Error: " + err);
 					$submitButton.removeAttr("disabled");
 				}
 			},