【问题标题】:Ajax request keeps returning error function in LaravelAjax请求在Laravel中不断返回错误函数
【发布时间】:2018-04-04 08:04:26
【问题描述】:

大家好。在我的 laravel 应用程序中,我试图检查我的表中是否存在特定 date、subject、grade 的出勤率。如果是这样,我有一个 if statement 设置来根据返回的内容显示期望的结果。

我正在使用 ajax 发出请求,但似乎 ajax 一直在运行错误函数,我似乎没有收到任何错误代码或 internal server error(500, 404, 403, etc) 在我的控制台中,状态返回是 200 ok

这是我的脚本:

$(document).on('change', '#subject', function(event) {
  event.preventDefault();
  /* Act on the event */

  var subject = $('#subject').val();
  var grade = $('#grade').val();
  var date = $('#date').val();

  if (subject != "" && grade != "") {
    $.ajax({
      url:"/attendence/students",
      method:"GET",
      data:{"subject_id":subject, "grade_id":grade, "date":date},
      dataType:"json",
      success:function(data){
        $("#result").html(data);
      },
      error:function(){
        $("#result").html('There was an error please contact administrator');
      }
    });
  }
});

这是请求发送到的控制器:

 public function students(Request $request)
{
    //
    $grade = Grade::findOrFail($request->grade_id);

    $subject = Subject::findOrFail($request->subject_id);

    $students = Student::where('grade_id', $grade->id)->get(['id', 'first_name','middle_name', 'surname', 'grade_id']);

    $statuses = Attendence::statuses();

    // this check if attendence has been setup for the given date.
    // if so prevent user for enter another date
    $attendenceExists = Attendence::where([
        'grade_id' => $grade->id, 
        'subject_id' => $subject->id, 
        'date' => $request->date
    ])->first();


    if ($attendenceExists) {
        return response()->json('A recorded attendence already exists for the seleced grade and subject!');
    } 
    else {
        return \View::make('attendence.partials.attendence-form')->with(array(
            'students' => $students,
            'subject' => $subject,
            'date' => $request->date,
            'statuses' => $statuses
        ));
    }

}

现在,如果此代码返回 true:

// this check if attendence has been setup for the given date.
    // if so prevent user for enter another date
    $attendenceExists = Attendence::where([
        'grade_id' => $grade->id, 
        'subject_id' => $subject->id, 
        'date' => $request->date
    ])->first();

   if ($attendenceExists) {
        return response()->json('A recorded attendence already exists for the seleced grade and subject!');
    } 

这里的条件运行并返回正确的结果。但是我上面的else statement 确实运行了,但我没有得到正确的结果。这是我得到的结果:

出现错误,请联系管理员

这表明正在运行的正是这部分 ajax 请求:

error:function(){
   $("#result").html('There was an error please contact administrator');
}

令人惊讶的是,当我检查控制台时,我看到的是:

这正是我想要的,但 ajax 否则返回。我做错了吗?

【问题讨论】:

    标签: javascript php jquery ajax laravel


    【解决方案1】:

    当您返回 html 时,您的 dataType 设置为 json。将其更改为html

    【讨论】:

    • 嗯,这是另一种简短的方法。谢谢!
    【解决方案2】:
    $.ajax({
          url:"/attendence/students",
          method:"GET",
          data:{"subject_id":subject, "grade_id":grade, "date":date},
          dataType:"json",
          statusCode: {
                200: function(data) {
                    $("#result").html(data.responseText);
                };
           }
          }
        });
    

    试试这个。希望对你有帮助

    【讨论】:

    • 为什么选择 data.responseText?
    • Laravel 在 responseText 中返回视图,尝试 console.log(data)
    • 是的,你可以,像这样 $.ajax({ statusCode: { 404: function() { alert( "page not found" ); } } });
    • 还有一件事。这意味着没有达到succes功能对吗?
    • 是的,但是你可以使用 $.ajax({ }).always(function(){});
    【解决方案3】:

    我会说根本不设置数据类型。只需完全删除该设置,让 jQuery ajax() 方法自动为您检测它。这样,如果响应类型是 JSON,它就会起作用。如果响应类型是 HTML,它也可以工作。 ??

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-08-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-12
      • 1970-01-01
      • 2015-06-02
      • 1970-01-01
      相关资源
      最近更新 更多