我通过将变量传递给ajax并在控制器中使用它来将数据保存到数据库中。Laravel不返回页面
保存工作正常,它将数据保存到数据库中,但问题是显示保存的页面。数据库有2个字段,名称和html。名称显然是网站的名称,而html是纯粹的html代码。因此,当用户保存页面名称“随机”我想用本地主机/随机
错误消息我得到的是显示HTML页面,他说:
试图获得的非对象(View属性:C :\ XAMPP \ htdocs中\ fyproject \资源\意见\布局\ website.blade.php)
这是我迄今所做的:
阿贾克斯:
var web_name;
function updateDatabase(newCode, name_website)
{
code2 = document.getElementById("content-link2").innerHTML;
web_name = ($('#website_name').val());
// make an ajax request to a PHP file
// on our site that will update the database
// pass in our lat/lng as parameters
$.post('http://localhost/template', {
_token: $('meta[name=csrf-token]').attr('content'),
newCode: (code2),
name_website: (web_name),
})
.done(function() {
})
.fail(function() {
alert("error");
});
}
控制器:
public function postDB(Request $request) {
$newName = $request->input('name_website');
$newLat = $request->input('newCode');
$websites = new Website();
$websites->name = $newName;
$websites->html = $newLat;
$websites->save();
$name = $newName;
return redirect($name);
}
public function website($name) {
$website = Website::where('name', $name)->first()->html;
// Render resources/views/template.blade.php or any view you want
// and pass the data. E.g. $website, so you can access $website->html in your view.
return view('layouts/website', compact('name'));
}
}
路线:
Route::get('home', '[email protected]');
Route::get('template', '[email protected]');
Route::post('template', '[email protected]');
Route::get('logout', '[email protected]');
Route::get('/{name}', '[email protected]');
website.blade.php:
@extends('layouts.master') @section('title', 'Website Builder') @section('content')
<meta name="csrf-token" content="{{ csrf_token() }}" />
{{html_entity_decode($name->html)}}
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script>
axios.get('/localhost/name').then(
html => document.querySelector('html').innerHTML = html
);
</script>
</html>
@endsection @show
这是怎么回事?
重定向($名)访问HTML;和window.location重新加载页面,你需要你的HTML而不重新加载页面?或者您只需在用户保存数据后显示结果? –
显示结果一旦用户保存数据 – Przemek