weixin_33711641 2017-10-02 19:30 采纳率: 0%
浏览 15

带有laravel和jQuery的Ajax

I am new to laravel and even more with ajax, I am trying to update from ajax but I can not do anything, it stays frozen and after a few seconds the url is updated with the data that happens to it, but in the base data nothing happens. If you could correct the code I would appreciate it because I am sure that this is the one that is giving problem because of my lack of experience, thank you very much

Route:

Route::name('appointments.update')->put('/appointment/$id', 'AppointmentController@update'); 

html:

<div class="modal fade" id="UpdateEventModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
    <div class="modal-content">
        <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                <span aria-hidden="true">&times;</span>
            </button>
            <h4 class="modal-title" id="myModalLabel">Update</h4>
        </div>  
         <form  class="form-horizontal">
            <div class="modal-body">
                @include('appointments._form-appointments')
            </div>
            <div class="modal-footer">
                <div class="col-md-12 form-group">
                {{ Form::submit('Update',['class' => 'btn btn-primary','id' => 'btn-modal-update',]) }}
                {{ Form::button('Close',['class' => 'btn btn-default', 'data-dismiss' => 'modal']) }}
                </div>
            </div>
        </form>
    </div>
</div>

ajax:

$(document).ready(function() {



    $('#btn-modal-update').click(function(){
        var id = $('#appointment_id').val();

        var route="{{url('appointments.update')}}/"+id+"";
        $.ajax({
            type:'PUT',
            headers:{"Authorization": localStorage.getItem('token')},
            url:route,
            dataType:'json',
            data: {startTime: $('input#startTime').val(), finalTime: $('input#finalTime').val(), patient_id: $('select[name="patient_id"').val(),
            user_id: $('select[name="user_id"').val(event.user_id), date: $('input#date').val(), appointment_id: $('#appointment_id').val()},
            success:function(data){
                if(data.success == 'true'){
                    alert('updated');
                }
            },
            error:function(data){
                alert('not updated');
            }
        });
    });

});

controller:

  public function update(Request $request, $id)
{
    if($request->ajax()){
    $appointment = Appointment::find($id);

    $appointment->date = $request->date;
    $appointment->startTime = $request->startTime; 
    $appointment->finalTime = $request->finalTime;
    $appointment->clinic_id = $request->clinic_id;
    $appointment->user_id = $request->user_id;    
    $appointment->patient_id = $request->patient_id;        

    $result = $appointment->save();
    if (result) {
        return response()->json(['success'=>'true']);
    }
    else{
         return response()->json(['success'=>'false']);
    }
}
}
  • 写回答

1条回答 默认 最新

  • bug^君 2017-10-03 00:10
    关注

    You should change your event listener a little bit:

    Listen to the submit() event on your form and make sure that you prevent the normal form submission by adding the following line at the end of the callback: return false;

    If it still isn't doing anythin, check your console for error output.

    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?