user9659025
user9659025

Reputation:

Symfony\Component\HttpKernel\Exception\HttpException- Ajax post request

In the step 2 of a multi step form, if a radio button is selected and "go to step 3" is clicked or if no radio button is selected and "go to step 3" is clicked it appears always this error below:

{message: "", exception: "Symfony\Component\HttpKernel\Exception\HttpException",…}
exception"Symfony\Component\HttpKernel\Exception\HttpException"
file "/Users/johnw/projects/store/vendor/laravel/framework/src/Illuminate/Foundation/Exceptions/Handler.php" line : 203 message: "" trace:[{,…},…]

Do you know where can be the issue?

Html for the step 2:

<form method="post" id="step2form" action="">
    <h6>Payment method</h6>
    <div class="form-group">
        <div class="form-check">
            <input class="form-check-input" type="radio" name="payment_method" value="option1">
            <label class="form-check-label d-flex align-items-center" for="exampleRadios1">
                <span class="mr-auto">Payment method 1</span>
            </label>
        </div>
        <br>
        <div class="form-check">
            <input class="form-check-input" type="radio" name="payment_method" value="option1">
            <label class="form-check-label d-flex align-items-center" for="exampleRadios1">
                <span class="mr-auto">Payment method 2</span>
            </label>
        </div>
    </div>
    <br>
    <div class="text-right">
    <button type="button" href="#step3" data-toggle="tab" role="tab"
            class="btn btn-outline-primary prev-step">
        Go back to step 2
    </button>
        <input type="submit" href="#step2" id="goToStep3"
         class="btn btn-primary btn float-right next-step"
               value="Go to step 3"/>
    </div>
</form>

PaymentController method to handle ajax request for step 2:

public function storePaymentMethods(Request $request, $id, $slug = null, Validator $validator){

        $validator = Validator::make($request->all(),[
            'payment_method' => 'required',
        ]);

        if($validator->passes())

        {
            return response()->json([
                'success' => true,
                'message' => 'success'
            ], 200);
        }
        $errors = $validator->errors();
        $errors =  json_decode($errors);

        return response()->json([
            'success' => false,
            'errors' => $errors
        ], 422);
    }

ajax:

   $('#goToStep3').on('click', function (event) {
              event.preventDefault();

              var custom_form = $("#" + page_form_id);

              $.ajax({
                  method: "POST",
                  url: '{{ route('products.storePaymentMethods', compact('id','slug') ) }}',
                  data: custom_form.serialize(),
                  datatype: 'json',
                  success: function (data, textStatus, jqXHR) {
                  },
                  error: function (data) {

                      var errors = data.responseJSON;
                      var errorsHtml = '';
                      $.each(errors['errors'], function (index, value) {
                          errorsHtml += '<ul class="alert alert-danger mt-3"><li class="text-danger">' + value + '</li></ul>';
                          console.log(errorsHtml);
                      });

                      $('#response').show().html(errorsHtml);
                  }
              });

Upvotes: 1

Views: 2341

Answers (1)

Jobayer
Jobayer

Reputation: 1231

you have not added any csrf token with your form. There are several ways to do it like below mentioned ways -

  • Add {{ csrf_field() }} inside your form
  • or add token with your ajax request like _token:"{{ csrf_token() }}"

Upvotes: 3

Related Questions