简体   繁体   English

是否有使用AJAX处理错误的更合适方法?

[英]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: 在我最初的带有程序代码的游戏中,我经常按照以下方式进行操作:

  • user visits a page, say a shop.php. 用户访问一个页面,例如shop.php。 On opening the site, shop.php retrieves from database list of items that are available to buy. 在打开站点时,shop.php从数据库中检索可购买的商品列表。 There are blank #success-message and #error-message paragraphs on the site. 该网站上有空白的#success-message#error-message段落。
  • user then can choose an item from shop he wants to buy- clicking the item triggers javascript, and an AJAX call is made to the buy-item.php , expecting a json to be returned 然后,用户可以从要购买的商店中选择一个项目-单击该项目将触发javascript,然后对buy-item.php进行AJAX调用,期望返回json
  • in buy-item.php I'm creating an array, say, $result("success" => 0, "message" => "") 在buy-item.php中,我正在创建一个数组,例如, $ result(“ success” => 0,“ message” =>“”)
  • buy-item.php checks if the player can afford the item, has space in backpack, etc., and if there's one condition that isn't met, then $result["message"] sets to, say, "You need more gold". buy-item.php检查玩家是否负担得起该物品,背包中是否有空间等,以及是否存在一个不满足的条件,则$ result [“ message”]设置为,例如,“您需要更多金”。 If all conditions are met, then result["success"] set's to 1, and $result["message"] to, say, "Purchase successful". 如果满足所有条件,则将result [“ success”]设置为1,将$ result [“ message”]设置为“购买成功”。 Finally, it echoes json_encode($result) to AJAX. 最后,它将json_encode($ result)回显到AJAX。
  • in AJAX success: I check if(result.success) , and act accordingly- If purchase was successful, I'd do $("#error-message").html(result.message) , and if it was successfull, I'd put result.message in "#success-message" instead (I could display both error and success messages in the same paragraph I guess, but that's simpler for me to do it separately, because these messages are styled differently, say, success is colored green, and error- red). 在AJAX 成功中:我检查if(result.success) ,并采取相应的行动-如果购买成功,我将执行$(“#error-message”)。html(result.message) ,如果成功,我而是将result.message放在“#success-message”中(我猜我可以在同一段中同时显示错误和成功消息,但这对我来说更容易分别执行,因为这些消息的样式不同,例如,成功颜色为绿色,错误为红色)。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM