weixin_33711641 2016-07-11 06:23 采纳率: 0%
浏览 71

Laravel Ajax中的错误500

I got this error which I tried to fix with no success

in console

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);
      }
    })
  });
});

Routes

 Route::post('/partA-edit-cost', 'CostController@editCost');

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);
 }

}

Disable temporarily stuff inside the editCost method will remove the error

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

  }//returns on "ok " to the console; 
  • 写回答

2条回答 默认 最新

  • YaoRaoLov 2016-07-11 06:33
    关注

    I think there is any error at $cost = COST::where('id', 3);, try changing it to

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

    $cost = COST::where('id', 3); results query scope not the Model, therefore you should add first() method, if no result matches your condition it will return null which can also result in the internal error issue since, you are calling $cost->type on the next line.

    I hope this helps.

    评论

报告相同问题?