我想添加一個帖子,而不使用頁面刷新使用最簡單的方法,通過使用jquery ajax。laravel ajax沒有發佈到數據庫
它沒有發佈到我的mysql數據庫,我不知道爲什麼。
我正沿着這個tutorial
Vue.js下看起來像什麼事情我可以在未來使用,但我只是不能換我的頭周圍
有什麼建議?
Dashboard.blade.php
@extends('layouts.layout')
@section('title')
Dashboard
@endsection
@section('content')
<div class="dashboard eli-main">
<div class="container ">
<div class="row">
<div class="col-md-6 col-md-12">
<h1>{{$user->username}}</h1>
<h4>What do you have to say?</h4>
<form>
<div class="form-group">
<textarea class="form-control" name="body" id="body" rows="5" placeholder="Your Post"></textarea>
</div>
<button type="submit" id="add" class="mybtn2">Create Post</button>
<input type="hidden" value="{{ Session::token() }}" name="_token">
</form>
@foreach($posts as $post)
<article class="post">
<h4>{{ $post->user->username }}</h4>
<p class="post-bod">
{{ $post->body }}
</p>
<div class="info">
made on {{ date('F d, Y', strtotime($post->created_at)) }}
</div>
</article>
@endforeach
</div>
</div>
</div>
</div>
App.js
$("#add").click(function() {
$.ajax({
type: 'post',
url: '/addItem',
data: {
'_token': $('textarea[name=_token]').val(),
'name': $('textarea[name=body]').val()
},
success: function(data) {
if ((data.errors)) {
$('.error').removeClass('hidden');
$('.error').text(data.errors.name);
} else {
$('.error').remove();
$('#table').append("<tr class='item" + data.id + "'><td>" + data.id + "</td><td>" + data.name + "</td><td><button class='edit-modal btn btn-info' data-id='" + data.id + "' data-name='" + data.name + "'><span class='glyphicon glyphicon-edit'></span> Edit</button> <button class='delete-modal btn btn-danger' data-id='" + data.id + "' data-name='" + data.name + "'><span class='glyphicon glyphicon-trash'></span> Delete</button></td></tr>");
}
},
});
$('#body').val('');
});
PostController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Post;
use App\User;
use Illuminate\Support\Facades\Auth;
use App\Http\Requests;
use Validator;
use Response;
use Illuminate\Support\Facades\Input;
class PostController extends Controller
{
public function getDashboard()
{
$posts = Post::orderBy('created_at', 'desc')->get();
$cookie = cookie('saw-dashboard', true, 15);
$users = User::all();
$user = new User();
// return view('dashboard', array('user'=> Auth::user()), compact('users'))->withCookie($cookie);
return view('dashboard',array('user'=> Auth::user(), 'posts' => $posts, compact('users')))->withCookie($cookie);
}
// public function postCreatePost(Request $request)
// {
// $this->validate($request,[
// 'body' => 'required|max:1000'
// ]);
// $post = new Post();
// $post->body = $request['body'];
// $message = 'There was an error';
// if($request->user()->posts()->save($post)){
// $message = 'Post Successfully Created';
// }
// return redirect()->route('dashboard');
// }
public function postCreatePost(Request $request) {
$rules = array (
'body' => 'required|max:1000'
);
$validator = Validator::make (Input::all(), $rules);
if ($validator->fails())
return Response::json (array (
'errors' => $validator->getMessageBag()->toArray()
));
else {
$post = new Post();
$post->body = $request->body;
$post->save();
return response()->json($post);
}
}
}
而且將R歐特是
Route::post ('/addItem', '[email protected]');
什麼是在控制檯中的錯誤?你檢查過嗎? – imrealashu
@imrealashu jquery.min.js:4 POST http:// localhost:8000/addItem 500(內部服務器錯誤) – BARNOWL
@OwlMan'postCreatePost'中的if子句沒有'{' – linktoahref