簡體   English   中英

將錯誤從控制器返回到 ajax 調用

[英]Returning error from controller to ajax call

我的控制器中有 switch case,我想將翻譯文本的錯誤變量返回到我的 ajax 調用中。 如何將我的錯誤消息返回到 ajax 調用中?

switch (true) {
        case (($to <= $from) && ($statistics > $from)):
            $error = [trans('report.errors.combine')];
            return $error;
            break;
        case ($to <= $from):
            $error = [trans('report.errors.to_date')];
            return $error;
            break;
        case ($statistics > $from):
            $error = [trans('report.errors.statistics')];
            return $error;
            break;
        default:
            $this->generate($request);
    }

你應該使用if , else if & else條件而不是switch case

   if(($to <= $from) && ($statistics > $from)){
        $error = [trans('report.errors.combine')];
    }
    elseif($to <= $from){
        $error = [trans('report.errors.to_date')];  
    }
    elseif($to <= $from){
        $error = [trans('report.errors.statistics')];
    }
    elseif($statistics > $from){
        $error = [trans('report.errors.statistics')];
    }
    else{
        $content = $this->generate($request);
    }

對於 AJAX 響應:

$res = (isset($error)) ? $error : $content;

header('Content-Type: application/json');
echo json_encode($res); exit();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM