我有一個laravel應用程序部署在我的服務器上。laravel ajax共享主機
文件結構
server
│
└───blog-app
│ │ app
│ │ bootstrap
│ │ ......
│
└───public_html
│ blog
│ │─── index.php
│
這是我的結構是什麼樣子。直到現在一切似乎都正常,但是現在我試圖對我的控制器功能進行AJAX調用,但它不能正常工作。
讓我用一個例子解釋,
$.ajax({
type:'POST',
url:'{{route('blog.prefetchResults')}}',
data:'_token = <?php echo csrf_token() ?>',
success:function(data){
$("#msg").html(data.msg);
}
});
這是我的AJAX調用都到它指向駐留在BlogController
控制器的路線blog.prefetchResults
。然而,當我看在瀏覽器控制檯我看到這種類型的錯誤:
http://mysubdomain.com/blog/[email protected] 500 (Internal Server Error)
我可以從這個錯誤推斷的是,它不允許當客戶端請求讓我來訪問這些目錄,這使得我問一個非常簡單的問題:如果我不能這樣做,我該如何使用laravel發出AJAX請求?
更新(例如):
Failed to load resource: the server responded with a status of 500 (Internal Server Error)
test.blade.php
@extends('main')
<p>
This is an ajxax test
</p>
<button class="butt">
click me
</button>
<script>
$().ready(function(){
$('butt').on('click',function(){
$.ajax({
type:'GET',
url:'{{route('blog.test')}}',
success:function(data){
$("p").html("somthing");
}
});
})
});
</script>
Web.php
//Test controller
Route::get('blog/test'.'[email protected]')->name('blog.test');
BlogController
public function test(){
return view('test.test')->withMessage('changed');
}
文件結構
文件結構
server
│
└───blog-app //Not accessible through client side languages
│ │ app->Http->Controllers/BlogController.php
│ │ resources->views->test->test.blade.php
│ │ routes->web.php->route defined here
│
└───public_html(www folder)// accessible to the client (www.mydomain.com)
│ blog
│ │─── index.php
│
@Alejandro回覆
- 校正了上述問題。
- 對不起關於
app->Http->BlogController.php
,它是app->Http->Controllers/BlogController.php
。
注:
public_html目錄是根目錄,這是對我的
mydomain.com
根點。所以其他文件夾如
blog-app
對於普通用戶來說是不可訪問的,它們只能被服務器端語言訪問。
日誌文件
[2017-01-08 19:23:01] local.ERROR: exception 'ErrorException' with message
'Trying to get property of non-object' in
/home/mydomainuser/blogapp/storage/framework/views/af6b0045a9d30a80c19dd552032d46b39ad1cc99.php:1
Next exception 'ErrorException' with message
'Trying to get property of non-object (View: /home/foodq7y4/blogapp/resources/views/blog/single.blade.php)' in
/home/mydomainuser/blogapp/storage/framework/views/af6b0045a9d30a80c19dd552032d46b39ad1cc99.php:1
的.htaccess
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule^index.php [L]
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</IfModule>
Single.blade.php
@extends('main')
@section('title', '| Blog')
@section('content')
<div class="row">
<div class="col-md-8 col-md-offset-2">
<h1>Blog</h1>
</div>
</div>
@foreach ($posts as $post)
<div class="row">
<div class="col-md-8 col-md-offset-2">
<h2>{{ ucwords($post->title) }}</h2>
<h5>Published: {{ date('M j, Y', strtotime($post->created_at)) }}</h5>
<p>{{ substr(strip_tags($post->body), 0, 250) }}{{ strlen(strip_tags($post->body)) > 250 ? '...' : "" }}</p>
<a href="{{ route('blog.single', $post->slug) }}" class="btn btn-sm btn-primary">Read More</a>
<hr>
</div>
</div>
@endforeach
<div class="row">
<div class="col-md-12">
<div class="text-center">
{!! $posts->links() !!}
</div>
</div>
</div>
@endsection
嘗試使用URL的實際路徑,而不是路由 – GabMic
它不會工作,要麼導致該目錄只與服務器端訪問 –
客戶端只被允許的的public_html –