【发布时间】:2017-08-11 19:47:47
【问题描述】:
我是Ajax 的新手,我正在按照教程使用Ajax 将数据发送到db
这是我的表格
{!! Form::open(array('url'=>'admin/blog', 'method'=>'post', 'files'=>'true')) !!}
<div class="box-body">
<div class="form-group">
{!! Form::label('title', 'Title') !!}
{!! Form::text('title', '', array('placeholder'=>'Blog title', 'class'=>'form-control')) !!}
</div>
<div class="form-group">
{!! Form::label('paragraph', 'Blog Content') !!}
{!! Form::textarea('paragraph', '', array('class'=>'form-control', 'placeholder'=>'Enter Paragraph...', 'rows'=>3)) !!}
<script>
CKEDITOR.replace('paragraph', {
uiColor: '#9AB8F3',
stylesSet: 'my_custom_style'
});
</script>
</div>
<div class="form-group">
{!! Form::label('image', 'Main Image') !!}
{!! Form::file('image') !!}
<p class="help-block">Please review the upload instructions in 'Reminder!'</p>
</div>
</div>
<div class="box-footer">
{!! Form::submit('Add', array('class'=>'btn btn-primary', 'onClick'=>'send(event)')) !!}
</div>
{!! Form::close() !!}
这是我使用的Ajax
<script type="text/javascript">
function send(event) {
event.preventDefault();
$.ajax({
type: "POST",
url: "{{ 'admin/blog' }}",
data: {
title: $("#title").val(),
paragraph: $("#paragraph").val(),
image: $("#image").val(),
_token: "{{ Session::token() }}"
},
success:function(result)//we got the response
{
alert('Successfully called');
},
error:function(exception){alert('Exeption:'+exception);}
})
}
</script>
这里是控制器
public function store(Request $request)
{
$this->validate($request, [
'title' => 'required',
'paragraph' => 'required|min:100',
'image' => 'required|image|mimes:jpeg,png',
]);
$add = new Blog();
if ($request->hasFile('image')) {
$image = $request->file('image');
$filename = time() . '.' . $image->getClientOriginalExtension();
Image::make($image)->resize(600, 390)->save(public_path('images/blog/' . $filename));
Image::make($image)->fit(335, 219)->save(public_path('images/blog/thumbs-' . $filename));
$add->image = $filename;
}
$add->title = $request->title;
$add->paragraph = $request->paragraph;
$add->addBy = \Auth::user()->name;
$add->save();
if ($request->ajax()) {
return response()->json();
}
return \Redirect::back();
}
当我尝试点击add时,我得到了
错误异常:[object Object]
编辑
在我的路线中,我使用resource 添加新的Route 和POST method
Route::resource('blog', 'BlogController');
Route::post('blog', 'BlogController@store');
更改了 Ajax URL 和错误结果
url: "{{ url('admin/blog/store') }}",
error:function(exception){console.log(exception)}
在我的控制台中出现此错误
Object { readyState: 4, getResponseHeader: .ajax/x.getResponseHeader()、getAllResponseHeaders: .ajax/x.getAllResponseHeaders(), setRequestHeader: .ajax/x.setRequestHeader(), overrideMimeType: .ajax/x.overrideMimeType(),状态码:.ajax/x.statusCode(),中止: .ajax/x.abort(),状态:.Deferred/d.state(),总是: .Deferred/d.always(),然后:.Deferred/d.then(),还有 11 个……}
【问题讨论】:
-
在这一行:
alert('Exeption:'+exception)异常是一个对象。因此,当您警告它时,它会被强制转换为字符串 - 这就是为什么alert()在调试时不是一个好主意。请改用console.log(exception),希望您能获得一些有用的信息来帮助诊断问题 -
你能明确你得到的错误信息吗?它在哪里?什么对象?
-
将
alert('Exeption:'+exception);替换为console.log('Exeption:'+exception)。然后发送日志,以便我们为您提供帮助 -
@AhmadRezk 我已将该行替换为
error:function(exception){console.log('Exeption:'+exception)},但我没有得到任何响应没有错误我应该从哪里得到这个log -
@AhmadRezk 我找到了这个
<span class="exception_title"><abbr title="Symfony\Component\HttpKernel\Exception\NotFoundHttpException">NotFoundHttpException</abbr> in <a title="C:\wamp\www\meshwebsite\vendor\laravel\framework\src\Illuminate\Routing\RouteCollection.php line 161" ondblclick="var f=this.innerHTML;this.innerHTML=this.title;this.title=f;">RouteCollection.php line 161</a>:</span>我知道它没有找到,请求网址是http://localhost:8000/admin/blog/admin/blog,应该是http://localhost:8000/admin/blog
标签: php jquery ajax laravel-5.2