2

Good day guys. In my laravel application I'm trying to check if attendence for a particular date, subject, grade exists in my table. If so I have an if statement setup to display desire results based on what is returned.

I'm making the request with ajax but it seems like ajax keeps running the error function and I don't seem to get any error code whatsoever or internal server error(500, 404, 403, etc) In my console the status return is 200 ok

here is my script:

$(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');
      }
    });
  }
});

Here is the controller the request is send to:

 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
        ));
    }

}

Now, if this code returns 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!');
    } 

The condition here runs and the right result is returned. But my else statement in the above does run but I don't get the right result. This is the result I get:

There was an error please contact administrator

Which shows that it is this part of the ajax request that is running:

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

Surprisingly when I check the console this is what I see:

enter image description here

Which is exactly what I want but ajax is return otherwise. Am I doing something wrong?

0

3 Answers 3

4

Your dataType is set to json while you're returning html. Change it to html.

Sign up to request clarification or add additional context in comments.

1 Comment

Well that's another short way to do it. Thanks!
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);
            };
       }
      }
    });

Try this. I hope this will help you

7 Comments

why data.responseText?
Laravel return view in responseText, try to console.log(data)
yes you can, something like this $.ajax({ statusCode: { 404: function() { alert( "page not found" ); } } });
One more thing. This means the succes function isn't reach right?
yes, but instead of you can use $.ajax({ }).always(function(){});
|
0

I would say don't set the dataType at all. Just remove that setting altogether and let the jQuery ajax() method detect it automatically for you. That way, if the response type is JSON, it'll work. If the response type is HTML, it'll also work. 👍🏻

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.