2016-08-10 26 views
0

我想添加一個評論系統在我的網站上使用AJAX與Laravel加載新評論而不刷新整個頁面。評論系統使用ajax和laravel無需重新加載頁面

我一直有這樣的錯誤:

Ajax Post 500 (Internal Server Error)

下面

是我的代碼:

view page : 
{!! Form::open(array('action' => array('[email protected]', $person->id))) !!}   
<div class="form-group"> 
{!! Form::label('name', 'Nom ') !!} 
{!! Form::text('name', null, ['class' =>'form-control', 'style'=>'border-radius: 0']) !!} 
</div> 

<div class="form-group"> 
{!! Form::label('mail', 'E-mail ') !!} 
{!! Form::text('mail', null, ['class' =>'form-control', 'style'=>'border-radius: 0']) !!} 
</div> 

<div class="form-group"> 
{!! Form::label('comment', 'Contenu ') !!} 
{!! Form::textarea('comment', null, ['class' =>'form-control', 'style'=>'border-radius: 0']) !!} 
</div> 

<div class="form-group"> 
{!! Form::submit('Publier', array('class'=>'send-btn')) !!}    
</div> 
{!! Form::close() !!} 

route page : 

Route::get('{id}/apropos','[email protected]'); 
Route::post('{id}/apropos','[email protected]'); 

控制器頁:

public function newComment($id) { 
    if ($request::ajax()) { 
     $name = Input::get('name'); 
     $mail = Input::get('mail'); 
     $comment = Input::get('comment'); 
     $Comments = new \App\Comment; 

     $Comments->Persons_id = $id; 
     $Comments->date = \Carbon\Carbon::now(); 
     $Comments->name=$name; 
     $Comments->mail=$mail; 
     $Comments->comment=$comment; 
     $Comments->save(); 

     $reponse =array(
      'status' => 'success', 
      'msg' => 'Setting created successfully', 
     ); 

     return \Response::json($response); 
    } else { 
     return 'no'; 
    } 
} 

Ajax頁面:

$.ajaxSetup({ 
    headers: { 
      'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') 
    } 
});  

$(document).ready(function() { 
    $('.send-btn').click(function (e) { 
     e.preventDefault(); 
     var name = $('#name').val(); 
     var mail = $('#mail').val(); 
     var comment = $('#comment').val(); 
     $.ajax({ 
      type: "POST", 
      url: '{{url("1/apropos")}}', 
      dataType: 'JSON', 
      data: {name: name, mail: mail, comment: comment}, 
      success: function(data) { 
      $("body").append("<div>"+data+"</div>");     
      } 
     }); 
    }); 
});   
+1

你有訪問錯誤日誌:

error callback option is invoked, if the request fails. It receives the jqXHR, a string indicating the error type, and an exception object if applicable. Some built-in errors will provide a string as the exception object: "abort", "timeout", "No Transport".

這樣你就可以在你的AJAX請求像補充的嗎?如果您將問題添加到您的問題中,這會讓我們更好地瞭解問題所在。只要發生嚴重錯誤,500錯誤可能意味着任何事情。 –

+0

我怎麼看?其中一個:.. storage \ logs \ laraval.log – nabil

+0

如果使用Apache,請找出您的Apache conf正在爲此項目傾銷日誌,並且您應該在Apache日誌文件中的最後一個錯誤 –

回答

0

我想你的ajax頁面與視圖在同一頁面(因爲你使用的是.send-btn)。

使用$ .post()會更容易,因爲您不需要配置如此多的參數。 https://api.jquery.com/jquery.post/

您是否有關於錯誤的更多信息:Ajax Post 500(內部服務器錯誤)。你沒有任何消息嗎?在$就使用誤差函數:

error: function (xhr, textStatus, thrownError) { 
    console.log(xhr.status); 
    console.log(xhr.statusText); 
    console.log(textStatus); 
    console.log(thrownError); 
    } 
+0

我沒有關於錯誤的更多信息:POST http:// localhost:82/doc/public/1/apropos 500(內部服務器錯誤) send @ jquery-2.1.0.min .js:4 ajax @ jquery-2.1.0.min.js:4 (anonymous function)@ apropos:1002 dispatch @ jquery-2.1.0.min.js:3 r.handle @ jquery-2.1。 0.min.js:3 我可以如何使用:$ .post() – nabil

+0

下面的日誌文件中的最後一個錯誤在apache :: 1 - - [10/Aug/2016:18:49:23 +0200 ]「POST/doc/public/1/apropos HTTP/1.1」500 26002 – nabil

+0

我發現這個問題,它是一個電子郵件字段加倍,它是單個字段(UNIQUE)! – nabil

相關問題