我在Bitnami WAMP堆栈上使用Laravel 5.5,PHP 7,当我通过GET将AJAX请求发送到命中控制器方法的路由时遇到404错误。
这是web.php:
Route::get('/', 'HomeController@index'); //works
Route::get('/loadstores/{$city}', 'HomeController@show'); //404
Route::view('/aboutus', 'aboutus'); //works
HomeController.php:
public function show($city) //never gets here
{
$stores = DB::table('stores')->where('city', $city);
return response()->json([
'success' => "ok"
]);
}
googlemaps.js:
function getIP(){
$.getJSON('http://ip-api.com/json/', function(result) {
console.log(result);
userIPlocation = {lat: result.lat, lng: result.lon};
userCity = result.city;
getStoresByCity();
});
}
function getStoresByCity(){
userCity = "new york"; //test
$.ajax({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
},
method:'GET',
url:'/loadstores',
data:{city:userCity},
dataType: 'JSON',
done:function(data){
console.log("hi"); //never gets here
console.log(data);
},
fail:function(xhr,status,error){
console.log("fail"); //never gets here
console.log(xhr,status,error);
}
});
}
这是Chrome控制台中的消息:http://localhost:8000/loadstores?city=new%20york 404(未找到)GET
我设置了csrf令牌,但我认为甚至不需要它,因为这是一个GET请求。
值得注意的是,我使用的是PHP artisan服务,因此Apache不会参与其中,因为这是我的.htcaccess,而且我从未重定向到google
.htcacesss:
<IfModule mod_rewrite.c>
Redirect 301 / https://google.com
<IfModule mod_negotiation.c>
Options -MultiViews -Indexes
</IfModule>
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
这是php artisan route:list:
+--------+----------+--------------------+------+-------------------------------------------+--------------+
| Domain | Method | URI | Name | Action | Middleware |
+--------+----------+--------------------+------+-------------------------------------------+--------------+
| | GET|HEAD | / | | App\Http\Controllers\HomeController@index | web |
| | GET|HEAD | api/user | | Closure | api,auth:api |
| | GET|HEAD | loadStores/{$city} | | App\Http\Controllers\HomeController@show | web |
| | GET|HEAD | aboutus | | \Illuminate\Routing\ViewController | web |
我已经多次清除了路由缓存。如果删除发送到/ loadstores的参数以及show()中的$ city,则会在页面上收到JSON成功消息。
非常困惑,谢谢!
答案 0 :(得分:1)
您的路由Route::get('/loadstores/{city}', 'HomeController@show');
表示它按照/loadstores/new-york
的模式将路由绑定到url,并将new-york
绑定到控制器中的变量$city
。
首先要注意的是,此处不需要路由定义中的$
,应将其删除,将路由更改为:Route::get('/loadstores/{city}', 'HomeController@show')
。
第二件事是您现在尝试绑定GET参数,而路由需要一个查询参数。如果您将网址更改为loadstores/new-york
,则应该可以使用。或者,如果您要使用GET参数,请将其更改为Route::get('/loadstores
,“ HomeController @ show”);`
答案 1 :(得分:0)
进行以下更改:
在您的 routes / web.php 中:
Route::get('/loadstores/{city}', 'HomeController@show');
在您的 HomeController 中:
public function show(Request $request,$city)
{
$stores = DB::table('stores')->where('city', $city)->get();
return response()->json([
'success' => "ok",
'stores'=>$stores
]);
}
在 googlemaps.js 中:
function getIP(){
$.getJSON('http://ip-api.com/json/', function(result) {
console.log(result);
userIPlocation = {lat: result.lat, lng: result.lon};
userCity = result.city;
getStoresByCity(userCity);
});
}
function getStoresByCity(userCity){
$.ajax({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
},
method:'GET',
url:`/loadstores/${userCity}`,
dataType: 'JSON',
success:function(data){
console.log(data);
},
error:function(xhr,status,error){
console.log(xhr,status,error);
}
});
}