2017-06-16 5 views
-1

Ich möchte eine Nachricht anzeigen, wenn der Ajax erfolgreich war oder nicht. Der Code funktioniert so weit, aber ich möchte einige Nachrichten hinzufügen.Laravel Ajax Nachricht anzeigen

My-Controller löschen Funktion:

public function destroy(Request $request, $streamID = 0) 
{ 
    $stream = Stream::find($streamID); 

    if($stream) 
    { 
     $stream->delete(); 
     File::delete($stream->image); 
     return redirect()->route('stream.index')->with('success', 'Hooray'); 
    } 
    else 
    { 
     return redirect()->route('stream.index')->with('success', 'Not Hooray'); 
    } 
} 

Und der Ajax-Erfolg und Fehler Teil:

 success: function(data) { 
      // show message? 
     }, 
     error: function(data) { 
      // show message? 
     } 
+0

Und wie kann Ihnen helfen? Erzähle ich dir von der Alarmfunktion? –

+0

Sie können Flash-Daten https://laravel.com/docs/5.4/session#flash-data – mfgabriel92

+0

anstelle von return redirect() -> route ('stream.index') -> mit ('success', 'Hooray '); Antwort zurückgeben() -> json ([' Erfolg '=>' Hurra ']); – sunilwananje

Antwort

1

Statt von Ihrem PHP-Code der Umleitung von Ihrer Ajax-Funktion umleiten und Json Antwort senden an Ihre ajax-Funktion als

public function destroy(Request $request, $streamID = 0) 
{ 
    $stream = Stream::find($streamID); 

    if($stream) 
    { 
     $stream->delete(); 
     File::delete($stream->image); 
     return response() ->json(['code'=>200,'success' => 'Hooray']); 
    } 
    else 
    { 
     return response() ->json(['code'=>400,'success' => 'Not Hooray']) 
    } 
} 

und in Ihrer ajax-Erfolg Funktion

success: function(response) { 
     alert(response.success); 
     window.location.href = 'stream.index'; 
}, 
error: function(data) { 
     // show message? 
} 
+0

Wie würden Sie es in Laravels Flash-Nachricht Stil zeigen (Box mit grün/rot Hintergrund) – utdev

+0

Versuchte Ihr Code nicht funktioniert – utdev

0

ich die vorherige Antwort auf diese aktualisiert:

.... 
    if($stream) { 
     $stream->delete(); 
     File::delete($stream->image); 
     return response()->json(['code'=>200,'success' => 'Hooray'],200); 
} 
    else { 
      return response()->json(['code'=>400,'success' => 'Not Hooray'],200); 
    } 
+0

Hallo, aber wo/wie füge ich die Erfolgsmeldung in der Ajax? – utdev

+0

@utdev Ich habe eine Antwort auf Ihren Kommentar geschrieben, hoffe, es hilft – hazelcodes

0

Wenn Sie Bootstrap verwenden, fügen Sie diese überall Sie Ihre Warnung vor

<div id="ajax-alert" class="alert" style="display:none"></div> 

Ändern Sie bitte Ihre Skript dies angezeigt werden soll :

success: function(response){ 
    If(response.code === 200) { 
     $('#ajax-alert').addClass('alert-sucess').show(function(){ 
      $(this).html(response.success); 
     }); 
    } 
    If(response.code === 400){ 
     $('#ajax-alert').addClass('alert-danger').show(function(){ 
      $(this).html(response.success); 
     }); 
    } 
}, 
error: function(data) { 
    alert ('Ajax run unsuccessful'); 
}