2016-07-11 9 views
0

Ich habe diesen Fehler, die ich ohne Erfolg zu beheben versuchtFehler 500 in Laravel Ajax

in Konsole

xhr.send(options.hasContent && options.data || null);//error 

js

var data_id; 
$(document).ready(function() { 
    $.ajaxSetup({ 
    headers: { 
     'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content') 
    } 
    }); 
    $(".edit_cost").click(function() { 
    data_id = $(this).closest('div.portlet').attr('data-id'); 
    }); 
    $(".submit_cost").on('click',function(e) { 
    e.preventDefault(); 
    var type = $(".cost_form").find('input[name="type"]').val(); 
    var cost = $(".cost_form").find('input[name="cost"]').val(); 
    var revenue = $(".cost_form").find('input[name="revenue"]').val(); 
    var profit = $(".cost_form").find('input[name="profit"]').val(); 
    var data = $("form.cost_form").serialize(); 
    alert(data); 
    $.ajax({ 
     url: "/partA-edit-cost", 
     type: "POST", 
     data: { 
     // 'types': type, 
     //'cost': cost, 
     // 'revenue': revenue, 
     // 'profit': profit, 
     // 'data_id': data_id 
     // 'data' : data 
     }, 
     error: function(data) { 
     console.log(data); 
     }, 
     success: function(log) { 
     console.log(log); 
     } 
    }) 
    }); 
}); 

Routen

Route::post('/partA-edit-cost', '[email protected]'); 

html

@foreach($costs as $widget) 
    <li data-row="1" data-col="1" data-sizex="3" data-sizey="3"> 
     <div class="portlet portlet-sortable light bordered ui-widget-content ui-resizable" data-id="{{$widget->id }}" data-find="{{$widget->id.$widget->name }}" data-name="{{ $widget->name }}"> 
     <div class="portlet-title ui-sortable-handle"> 
      <div class="caption font-green-sharp"> 
      <i class="fa fa-money"></i> 
      <span class="caption-subject bold uppercase">{{ $widget->name }}</span> 
      </div> 
      <div class="actions"> 
      <a href="javascript:;" class="btn btn-circle btn-default btn-sm remove"> 
      <i class="fa fa-trash-o"></i> Remove </a> 
      <div class="btn-group"> 
       <button type="button" class="btn btn-primary edit_cost" data-toggle="modal" data-target="#cost"><i class="fa fa-edit" data-button ="{{$widget->id}}"></i>Edit</button> 
      </div> 
      <a class="btn btn-circle btn-icon-only btn-default fullscreen" href="javascript:;"></a> 
      </div> 
     </div> 
     <div class="portlet-body"> 
      <div>{!! html_entity_decode($widget->api) !!}</div> 
     </div> 
     </div> 
    </li> 
    @endforeach 



    <div class="modal fade bs-example-modal-sm cost" id="cost" tabindex="-1" role="dialog" aria-labelledby="mySmallModalLabel"> 
    <div class="modal-dialog modal-sm" id="cost"> 
    <div class="modal-content"> 
     <div class="col-md-12" style="width:500px;"> 
     <div class="portlet box white"> 
      <div class="portlet-title"> 
      <div class="caption"> 
       <i class="fa fa-gift"></i>Edit Cost 
      </div> 
      </div> 
      <div class="portlet-body form"> 
      <!-- BEGIN FORM--> 
      <form action="/partA-edit-cost" class="cost_form" method="POST"> 

       <div class="form-actions top"> 
       <button type="submit" class="btn green submit_cost">Submit</button> 
       <button type="button" class="btn default">Cancel</button> 
       </div> 
       <div class="form-body"> 
       <div class="form-group"> 
        <label class="control-label">Type</label> 
        <input type="text" name="type" class="form-control type" placeholder="Enter Cost type"> 
       </div> 
       <div class="form-group"> 
        <label class="control-label">Cost</label> 
        <input type="text" name ="cost" class="form-control" placeholder="Enter Cost"> 
       </div> 
       <div class="form-group"> 
        <label class="control-label">Revenue</label> 
        <input type="text" name="revenue" class="form-control" placeholder="Enter Revenue"> 
       </div> 
       <div class="form-group"> 
        <label class="control-label">Profit</label> 
        <input type="text" name="profit" class="form-control" placeholder="Enter Profit"> 
       </div> 
       </div> 
      </form> 
      </div> 
     </div> 
     </div> 
    </div> 
    </div> 
</div> 

Controller

namespace App\Http\Controllers; 

use Illuminate\Http\Request; 

use App\Http\Requests; 
use App\Models\Cost; 

class CostController extends Controller 
{ 
public function editCost(Request $request) 
{ 
    $type = $request->get('type'); 
    //$id = $request->get('id'); 
    $cost = COST::where('id', 3); 
    $cost->type = "type"; 
    $cost->save(); 
    return \Response::json(['id' => $id,'type' => $type],200); 
} 

} 

vorübergehend deaktivieren Sachen innerhalb des editCost Methode wird den Fehler beseitigen

public function editCost(Request $request) { 
     echo "ok"; 

    }//returns on "ok " to the console; 

Antwort

0

Ich denke, es ist ein Fehler bei $cost = COST::where('id', 3);, versuchen Sie es zu

Ändern
$cost = Cost::where('id', 3)->first(); 

$cost = COST::where('id', 3); Ergebnisse Abfrage-Bereich nicht das Modell, daher sollten Sie first() Methode hinzufügen, wenn kein Ergebnis entspricht Ihrer Bedingung wird Null zurückgeben, was auch zu dem internen Fehler führen kann, da Sie in der nächsten Zeile $cost->type aufrufen.

Ich hoffe, das hilft.

+0

ja habe ich bereits versucht, dies wird tatsächlich Daten in der Datenbank speichern, aber nicht den Fehler in der Konsole entfernen –

+0

Bitte fügen Sie den Fehler t Rennen sehen wir, was wir helfen können –

0

In Ihrem Code die Variable $ id nicht in Ihrer return-Anweisung definiert ist (Sie haben es aus kommentiert)

return \Response::json(['id' => $id,'type' => $type],200); 

ich den Code denken sollte wie folgt aussehen:

$type = $request->get('type'); 
//$id = $request->get('id'); 
$cost = Cost::where('id', 3)->first(); 
$cost->type = "type"; 
$cost->save(); 
return \Response::json(['id' => 3,'type' => $type],200); 

Wenn das funktioniert Sie können die dynamische $ ID und $ Typ Variablen

Verwandte Themen