我尝试使用laravel 5在我的项目中使用ajax,它可以正常工作,但前提是我将这个函数放在如下的路径文件中:
Route::post('/post/post_up',function(){
if (Request::ajax()){
$raa=Input::get('_user');
if ($raa=='null') {
return 'Vous devez connecter';
}else{
$already_vote = \App\Post_vote::where('user_id','=',Input::get('_user'))->where('post_id','=',Input::get('_post'))->count();
if ($already_vote==0) {
$post = new \App\Post_vote;
$post->post_id=Input::get('_post');
$post->user_id=Input::get('_user');
$post->vote=1;
$post->save();
return 'Votre vote bien enregistre';
}else{
return 'Vous avez déja voté pour ce sujet !';
}
}
}
});
但是当我尝试将此功能从routes.php移动到我的控制器时它不起作用,我不知道为什么它在routes.php上工作而且在我的控制器中不起作用。这就是我的所作所为:
1。在Routes.php上:
Route::post('/post/post_up','VoteController@post_up');
2。 VoteController.php:
<?php namespace App\Http\Controllers;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class VoteController extends Controller {
public function post_up()
{
if (Request::ajax()){
$raa=Input::get('_user');
if ($raa=='null') {
return 'Vous devez connecter';
}else{
$already_vote = \App\Post_vote::where('user_id','=',Input::get('_user'))->where('post_id','=',Input::get('_post'))->count();
if ($already_vote==0) {
$post = new \App\Post_vote;
$post->post_id=Input::get('_post');
$post->user_id=Input::get('_user');
$post->vote=1;
$post->save();
return 'Votre vote bien enregistre';
}else{
return 'Vous avez déja voté pour ce sujet !';
}
}
}
}
}
第3。 Html和AJAX
<form id="post_up" action="#">
<input type="hidden" name="_token" id="_token" value="{{ csrf_token() }}">
<input type="hidden" name="_post" id="_post" value="{{ $post->id }}" />
@if (!Auth::guest())
<input type="hidden" name="_user" id="_user" value="{{ Auth::user()->id }}" />
@else
<input type="hidden" name="_user" id="_user" value="null" />
@endif
<button type="submit" class="btn btn-default" aria-label="Left Align">
<span class="glyphicon glyphicon-menu-up" aria-hidden="true"></span>
</button>
</form>
<script type="text/javascript" src="http://localhost:8080/js/jquery-2.2.1.js"></script>
<script type="text/javascript">
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
$(document).ready(function(){
//pour vote up
$('#post_up').submit(function(e){
e.preventDefault();
var _post = $('#_post').val();
var _user = $('#_user').val();
var _token = $('input[name="_token"]').val();
var data = new FormData();
data.append('_token',_token);
data.append('_user',_user);
data.append('_post',_post);
$.ajax({
type:"POST",
method:"POST",
url:"post_up",
data:data,
processData: false,
contentType: false,
success: function(data){
alert(data);
}
});
}); });</script>
答案 0 :(得分:0)
你必须在ajax url param中传递正确的URL ...
$(document).ready(function(){
//pour vote up
$('#post_up').submit(function(e){
e.preventDefault();
var _post = $('#_post').val();
var _user = $('#_user').val();
var _token = $('input[name="_token"]').val();
var data = new FormData();
data.append('_token',_token);
data.append('_user',_user);
data.append('_post',_post);
$.ajax({
type:"POST",
method:"POST",
url:"/post/post_up",
data:data,
processData: false,
contentType: false,
success: function(data){
alert(data);
}
});
}); });