2017-04-01 10 views
0

Ich versuche, Bild mit Ajax in Laravel 5.2 hochladen. Aber immer noch bekomme ich Fehler 500 Interner Server Fehler in der Route. wenn ich versuche, Bild mit Ajax-Anfrage hochladen den Browser angezeigte korrekte Route Pfad, aber immer noch bekomme ich keinen Grund, warum es immer noch Fehler zu mir zeigt.Laravel Image Upload mit Ajax 500 Interner Server Fehler

HTML

<!-- CHANGE AVATAR TAB --> 
         <div class="tab-pane" id="tab_1_2"> 

          <div class="uploadimagediv"></div> 
           {{ Form::open(array('url' => 'admin/avatar','method'=>'post','files'=>'true','id'=>'updateprofileimage')) }} 

           <div class="form-group"> 
            <div class="fileinput fileinput-new" data-provides="fileinput"> 
             <div class="fileinput-new thumbnail" style="width: 200px; height: 150px;"> 
              <img src="http://www.placehold.it/200x150/EFEFEF/AAAAAA&amp;text=no+image" alt=""/> 
             </div> 
             <div class="fileinput-preview fileinput-exists thumbnail" style="max-width: 200px; max-height: 150px;"> 
             </div> 
             <div> 
              <span class="btn default btn-file"> 
              <span class="fileinput-new"> 
              Select image </span> 
              <span class="fileinput-exists"> 
              Change </span> 
              <p class="text-danger" id="error_image"></p> 
              <input type="file" id="picture" name="picture"/> 
              {{--{{ Form::file('picture') }}--}} 
              </span> 
              <span class="error alert-danger">{{ $errors->first('picture') }}</span> 
              <a href="javascript:;" class="btn default fileinput-exists" data-dismiss="fileinput"> 
              Remove </a> 
             </div> 
            </div> 
            <div class="clearfix margin-top-10"> 

            </div> 
           </div> 
           <div class="margin-top-10"> 
            {{Form::hidden('id', 2, ["id"=>"id"])}} 
            {{ Form::button('Upload',['id'=> 'updatepicture','class'=>'btn green-haze']) }} 

            {{--{{ Form::submit('Submit',['class' => 'btn green-haze','name'=>'changeImage']) }}--}} 
            <a href="javascript:;" class="btn default"> 
            Cancel </a> 
           </div> 
          {{ Form::close() }} 
         </div> 
         <!-- END CHANGE AVATAR TAB --> 

Strecke

Route::group(['prefix' => 'admin'], function() 
{ 
    Route::controller('/','DashboardController'); 
}); 

Ajax

$(document).on('click', '#updatepicture', function($e) 
{ 
    $e.preventDefault(); 
    // send an ajax request 
    $("#error_image").empty(); 
    $.ajax(
    { 

     url: 'avatar', 
     processData: false, 
     contentType: false, 
     type: "post",//use for update 
     data: new FormData($("#updateprofileimage")[0]), 
     success: function(data) 
     { 
      if(data.status) 
      {   
       $("#uploadimagediv").html('<div class="alert alert-success"><button type="button" class="close">×</button>'+data.message+'</div>'); 
       window.setTimeout(function() 
       { 
        $(".alert").fadeTo(500, 0).slideUp(500, function() 
        { 
         $(this).remove(); 
        }); 
       }, 5000); 
       $('.alert .close').on("click", function(e) 
       { 
        $(this).parent().fadeTo(500, 0).slideUp(500); 
       }); 
       //console.log(data); 
       //$("#updateprofileimage")[0].reset(); 
       //window.location.href = "http://localhost/laravel/admin/profile"; 
      } 
      else 
      { 
       errors = data.errors; 

       for(error in errors) 
       { 
        $("#error_"+error.title).html(error.message); 
       } 
      } 
     }, 
     error: function(xhr) 
     { 
      if(xhr.status == 422) 
      { 
       errors = xhr.responseJSON; 
       for(error in errors) 
       { 
        $("#error_"+error).html(errors[error][0]); 
       } 
      } 
     } 
    }); 
}); 

Fehler ist: "NetworkError: 500 Interner Serverfehler - http://localhost/laravel/admin/avatar"

Bitte schlagen Sie mir vor, wo ich falsch liege.

Controller ist

public function postAvatar(ImageUploadRequest $request) 
{ 
    --- 
} 

Antwort

1
Add the below line inside <head> 
<meta name="csrf-token" content="{{ csrf_token() }}"> 

And add the below lines before your ajax call in javascript function 
$.ajaxSetup({ 
     headers: { 
      'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') 
     } 
}); 

And don't forget to give permission to your storage folder 
sudo chmod -R 777 storage 
+0

ich noch immer Fehler "NetworkError: 500 Internal Server Error - http: // localhost/Laravel/admin/avatar" –

+0

Dank für die Antwort –

+0

Sieht so aus, als hättest du deine Route nicht richtig in Ajax URLs gesetzt, in der Nähe von $ .ajax ( { URL: 'Avatar', es muss URL sein: '{{route (' name ')}}' –

1

In Ihrer Ajax-Setup Sie x-CSRF-Token mit dem Antrag zur Verfügung stellen müssen. Für Ihre Ajax-Anforderung, auch gibt es ein Problem mit Ihrer URL:

$(document).on('click', '#updatepicture', function($e) 
    { 
     $e.preventDefault(); 
     // send an ajax request 
     $("#error_image").empty(); 
     $.ajax(
     { 

      url: "{{url('avatar')}}", 
      processData: false, 
      contentType: false, 
      type: "post",//use for update 
      data: new FormData($("#updateprofileimage")[0]), 
      headers: { 
      'X-CSRF-TOKEN': "{{ csrf_token() }}" 
      }, 
      success: function(data) 
      { 
       if(data.status) 
       {   
        $("#uploadimagediv").html('<div class="alert alert-success"><button type="button" class="close">×</button>'+data.message+'</div>'); 
        window.setTimeout(function() 
        { 
         $(".alert").fadeTo(500, 0).slideUp(500, function() 
         { 
          $(this).remove(); 
         }); 
        }, 5000); 
        $('.alert .close').on("click", function(e) 
        { 
         $(this).parent().fadeTo(500, 0).slideUp(500); 
        }); 
        //console.log(data); 
        //$("#updateprofileimage")[0].reset(); 
        //window.location.href = "http://localhost/laravel/admin/profile"; 
       } 
       else 
       { 
        errors = data.errors; 

        for(error in errors) 
        { 
         $("#error_"+error.title).html(error.message); 
        } 
       } 
      }, 
      error: function(xhr) 
      { 
       if(xhr.status == 422) 
       { 
        errors = xhr.responseJSON; 
        for(error in errors) 
        { 
         $("#error_"+error).html(errors[error][0]); 
        } 
       } 
      } 
     }); 
    }); 
+0

Ich bekomme immer noch Fehler "NetworkError: 500 Interner Server Fehler - http: // localhost/Laravel/Admin/Avatar" –

+0

Vielen Dank für Ihre Antwort ... –

+0

@ashishbansal Antwort bearbeitet –

Verwandte Themen