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

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.

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算