Why is this page returning in JSON?
laravel相当新,我的应用程序工作得很好,除了一件事,
当我去创建一个新帖子时,它无法保存到数据库并返回请求JSON页面。
非常感谢任何解决此问题的帮助!
我认为这个问题可能与雄辩有关,我所拥有的代码似乎无法找到错误。
@extends('main')
@section('title', '| Create New Post')
@section('stylesheets')
{!! Html::style('css/parsley.css') !!}
{!! Html::style('css/select2.min.css') !!}
<script src="//cdn.tinymce.com/4/tinymce.min.js"></script>
<script>
tinymce.init({
selector: 'textarea',
plugins: 'link code',
menubar: false
});
</script>
@endsection
@section('content')
<div class="row">
<div class="col-md-8 col-md-offset-2">
<h1>Create New Post</h1>
<hr>
{!! Form::open(array('route' => 'posts.store', 'data-parsley-validate' => '')) !!}
{{ Form::label('title', 'Title:') }}
{{ Form::text('title', null, array('class' => 'form-control', 'required' => '', 'maxlength' => '255')) }}
{{ Form::label('slug', 'Slug:') }}
{{ Form::text('slug', null, array('class' => 'form-control', 'required' => '', 'minlength' => '5', 'maxlength' => '255') ) }}
{{ Form::label('category_id', 'Category:') }}
<select class="form-control" name="category_id">
@foreach($categories as $category)
<option value='{{ $category->id }}'>{{ $category->name }}</option>
@endforeach
</select>
{{ Form::label('tags', 'Tags:') }}
<select class="form-control select2-multi" name="tags[]" multiple="multiple">
@foreach($tags as $tag)
<option value='{{ $tag->id }}'>{{ $tag->name }}</option>
@endforeach
</select>
{{ Form::label('body', "Post Body:") }}
{{ Form::textarea('body', null, array('class' => 'form-control')) }}
{{ Form::submit('Create Post', array('class' => 'btn btn-success btn-lg btn-block', 'style' => 'margin-top: 20px;')) }}
{!! Form::close() !!}
</div>
</div>
@endsection
@section('scripts')
{!! Html::script('js/parsley.min.js') !!}
{!! Html::script('js/select2.min.js') !!}
<!-- <script type="text/javascript">
$('.select2-multi').select2();
</script> -->
@endsection
&#13;
创建帖子的控制器功能就是这个,
public function create()
{
$categories = Category::all();
$tags = Tag::all();
return view('posts.create')->withCategories($categories)->withTags($tags);
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
dd($request);
// validate the data
$this->validate($request, array(
'title' => 'required|max:255',
'slug' => 'required|alpha_dash|min:5|max:255|unique:posts,slug',
'category_id' => 'required|integer',
'body' => 'required'
));
// store in the database
$post = new Post;
$post->title = $request->title;
$post->slug = $request->slug;
$post->category_id = $request->category_id;
$post->body = Purifier::clean($request->body);
$post->save();
$post->tags()->sync($request->tags, false);
Session::flash('success', 'The blog post was successfully save!');
return redirect()->route('posts.show', $post->id);
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
答案 0 :(得分:1)
您的store()
方法正在做的第一件事是:dd($request)
。
dd()
代表“dump and die”,它会转储传递给它的任何内容,然后调用die()
。您看到的输出是$request
变量的转储,然后脚本就会死掉。
答案 1 :(得分:1)
如上所述,dd($ request)返回请求的JSON值,您应该首次使用它来查看表单中是否传递了所有值,然后您应该省略它,将其用于测试目的仅