jeudi 1 novembre 2018

Laravel: Getting error response from controller but type error on console and response message is not showing on view page

In my laravel project I am trying to get error response from controller and show on my view page. I have added the functionalities but I am getting TypeError in jquery. When I print response data on console, In console response message is showing. Here I have added jquery, response from controller and console messages. Any help would be appreciated.

jquery

$.ajax({
       type: "POST",
       url: url,
       data: $("form").serialize() + '&' + $.param({'formPart': $btn.val()}),
       dataType: 'JSON'
       })
         .done(function (data){
           $btn.button('reset');
           if (data.response === 'success') {
                window.location.href = '/test/url';
           }
       })
         .fail(function(data, jqxhr, textStatus, error) {
           if( data.status === 422 ) {
              console.log(data);
              var errors       = data.responseJSON.error;
              var bookingOrder = data.responseJSON.bookingOrder;
              if(errors) {
                $( "#errors_"+bookingOrder ).show();

                errorsHtml = '<div class="alert alert-danger"><ul>';
                $.each( errors , function( key, value ) {
                  errorsHtml += '<li>' + value + '</li>';
                });
                errorsHtml += '</ul></div>';

                $( "#errors_"+bookingOrder ).html( errorsHtml );
              }
              else {
                    var errData = data.responseJSON;

                    $.each(errData, function (i, item) {
                    var spliKey = i.split('.');
                    var fname   = spliKey[0];
                    $('input[name^="' + fname + '" ]').each(function (k, v) 
                    {
                     if (spliKey[1] == k) {
                       $(this).parent('.form-group').children('.help-block').html('<strong>' + item[0] + '</strong>');
                       $(this).parent('.form-group').addClass('has-error');
                      }
                    });
                     $('select[name^="' + fname + '" ]').each(function (k, v) {
                      if (spliKey[1] == k) {
                         $(this).parent('.form-group').children('.help-block').html('<strong>' + item[0] + '</strong>');
                         $(this).parent('.form-group').addClass('has-error');
                       }
                     });
                 });

                  $btn.button('reset');
             }
          }
       });

Response from laravel controller

if($monthBegin < $monthEnd) {
   $test[] = $dateDifference->days;
}
else {
      return response()->json(['error' => 'Arrival date should be less than departure date', 'bookingOrder' => 2], 422);
     }

Console When I print console.log(data). On console response is showing

responseJSON: Object { error: "Arrival date should be less than departure date.", bookingOrder: 2 }

responseText: "{\"error\":\"Arrival date should be less than departure date.\",\"bookingOrder\":2}"

Error showing in console

TypeError: cannot use 'in' operator to search for 'length' in 'Arrival date sho...'



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire