[英]Is there a more proper way to handle errors using AJAX?
I've never asked anything here, but there's something that bugs me. 我从没在这里问过任何问题,但是有些问题困扰着我。
You see, I'm toying with PHP for about a year. 您看,我在用PHP玩了大约一年。 I've made myself quite a big project while learning PHP, JQuery, MySQL, etc.- it's far from being perfect, or even being finished, but it works and I'm constantly trying to improve existing code while adding new functions. 在学习PHP,JQuery,MySQL等时,我已经做了一个很大的项目-它远非完美,甚至还没有完成,但是它有效,并且我一直在尝试在增加新功能的同时改进现有代码。 It's a text-based browser MMORPG game (it's still in development), if you want a link, I'll provide it, but I didn't come here to promote it. 这是一个基于文本的浏览器MMORPG游戏(仍在开发中),如果您想要一个链接,我会提供,但是我没有来这里进行宣传。
The thing is, I feel okay in procedural PHP (though I still have problems with repeating code), so I've decided it's about time to move to OOP. 事实是,我在程序PHP中感觉还不错(尽管我仍然在重复代码方面遇到问题),所以我认为现在是时候转向OOP了。 I understand it in theory, know the basics, and I'm slowly advancing in practice, but I wanted your advice, before moving too far. 我从理论上理解它,知道一些基础知识,并且在实践中我正在慢慢地进步,但是在走得太远之前,我需要您的建议。
In my original game with procedural code, I'm often doing something along these lines: 在我最初的带有程序代码的游戏中,我经常按照以下方式进行操作:
Now, is that a correct approach to handle errors? 现在,这是处理错误的正确方法吗? Or should I code it completely differently? 还是应该完全不同地编码? Will this approach be still valid in OOP? 这种方法在OOP中仍然有效吗?
Let me show you what I mean with this example, I'm toying with at the moment ( for simplicity sake, please forget about security or even usefulness of the code ): 让我向您展示这个示例的含义,此刻我正在玩弄( 为简单起见,请忘记代码的安全性甚至有用性 ):
index.php index.php
<!DOCTYPE html>
<html lang="pl-PL">
<head>
<meta charset="UTF-8">
<script
src="https://code.jquery.com/jquery-3.2.1.min.js"
integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4="
crossorigin="anonymous">
</script>
<script src="script.js"></script>
</head>
<body>
<div id="wrapper">
<p id="error-message"></p>
<p id="success-message"></p>
<form id="registration-form" method="post">
<input type="text" placeholder="Account Name" name="nickname"></input>
<input type="text" placeholder="Password" name="password"></input>
<input type="text" placeholder="E-mail" name="email"></input>
<button type="submit">Register</button>
</form>
</div>
</body>
</html>
User.php User.php
<?php
class User{
//register method
public function register($nickname, $password, $email){
$result = array("success" => 0, "message" => "");
//example validation
if($nickname==123){
$result["message"] = "Could not register.";
}
else{
//add user to the database
//sql code here
$result["success"] = 1;
$result["message"] = "Registration successful.";
}
return $result;
}
}
?>
registerProcessing.php registerProcessing.php
<?php
require_once 'User.php';
$user = new User();
echo json_encode($user->register($_POST["nickname"], $_POST["password"], $_POST["email"]));
?>
script.js script.js
$(function() {
$("#wrapper").on("submit", "#registration-form", function(e){
e.preventDefault();
$.ajax({
type: "POST",
url: "registerProcessing.php",
data: $("#registration-form").serialize(),
dataType: "json",
success: function(result){
if(result.success){
$("#success-message").html(result.message);
}
else{
$("#error-message").html(result.message);
}
}
});
});
});
The proper way is study the ajax callbacks and states already made into javascript/jQuery and play with them. 正确的方法是研究已经制作成javascript / jQuery的ajax回调和状态,然后使用它们。 If you need more advanced kind of feature, will need to send by backend some headers forcing some statuses that can be handled by the proper callbacks or customized ones. 如果您需要更高级的功能,则需要通过后端发送一些标头,以强制某些状态,这些状态可以由适当的回调或自定义的回调处理。
You can use the HTTP status codes mainly treated with major js libraries/frameworks (even the XMLHttpRequest pure itself that follow the HTTP pattern of codes, see MDN ). 您可以使用主要由主要js库/框架处理的HTTP状态代码 (甚至是遵循HTTP代码模式的XMLHttpRequest 纯自身,请参阅MDN )。
Directly answering your question, no, is not right. 直接回答您的问题,不,是不对的。 Your ajax is using a main callback (success) and handling by returned values. 您的ajax使用主回调(成功)并按返回值进行处理。 It's works, but lacks best practices about callbacks, like I mentioned above. 它是可行的,但是缺少有关回调的最佳实践,就像我上面提到的那样。
An example what you can do: 您可以执行的示例:
$("#wrapper").on("submit", "#registration-form", function(e){
e.preventDefault();
$.ajax({
type: "POST",
url: "registerProcessing.php",
data: $("#registration-form").serialize(),
dataType: "json",
success: function(result){
$("#success-message").html(result.message);
},
error: function(result){
$("#error-message").html(result.message);
}
});
});
or 要么
$("#wrapper").on("submit", "#registration-form", function(e){
e.preventDefault();
$.ajax({
type: "POST",
url: "registerProcessing.php",
data: $("#registration-form").serialize(),
dataType: "json",
statusCode: {
404: function() {
$("#error-message").html('NOT FOUND');
},
200: function(result) {
$("#success-message").html(result.message);
},
500: function(result) {
$("#error-message").html(result.message);
}
}
});
});
Or with XMLHTTPREQUEST example: 或以XMLHTTPREQUEST为例:
var request;
if(window.XMLHttpRequest)
request = new XMLHttpRequest();
else
request = new ActiveXObject("Microsoft.XMLHTTP");
request.open('GET', 'http://www.mozilla.org', false);
request.send(); // there will be a 'pause' here until the response to come.
// the object request will be actually modified
switch (request.status){
case 404:
break;
case 500:
break;
case 200;
break
}
To add to @CarlosAlexandre's answer about handling errors using HTTP status codes: for your transition to OOP programming, you should be aware that it is becoming a best practice to use exception handling to pass error conditions around. 要添加到@CarlosAlexandre关于使用HTTP状态代码处理错误的答案:在过渡到OOP编程时,您应该意识到,使用异常处理来传递错误条件已成为一种最佳实践。 This could like something like this: 这可能是这样的:
User.php User.php
class User{
//register method
public function register($nickname, $password, $email){
//example validation
if($nickname==123){
throw new InvalidArgumentException('Invalid username');
}
//add user to the database
//sql code here
}
}
registerProcessing.php registerProcessing.php
require_once 'User.php';
$user = new User();
$response = array(
'success' => 0,
'message' => 'Unknown error',
);
try {
$user->register($_POST["nickname"], $_POST["password"], $_POST["email"]);
$response['success'] = 1;
$response['message'] = 'Registration successful.';
} catch (Exception $exception) {
$response['message'] = $exception->getMessage();
}
echo json_encode($response);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.