Ajax 发生错误无法正常工作
Ajax is having an error not working
当用户第一次访问索引路径上的网站时,我将获取用户的时区。
我在我的 index.blade.html 文件中有一个 ajax 请求。 "/"
我正在将 ajax 路线发布到这条路线“/custom_sessions”。
获取错误
jquery-1.12.4.min.js:4 POST http://127.0.0.1:8000/custom_sessions 500(内部服务器错误)
// route the ajax is hitting
Route::post('/custom_sessions', 'CustomSessionsController@store');
-
// controller
class CustomSessionsController extends Controller
{
public function store()
{
// when i remove this line the ajax request is successful with no errors in the console
session('timezone' => $request('timezone'));
}
}
-
// have this in my head in my html
<meta name="csrf-token" content="{{ csrf_token() }}">
-
// ajax script at the bottom of my index.blade.php file
$.ajax({
headers: {
"X-CSRF-TOKEN": $('meta[name="csrf-token"]').attr('content')
},
type: "POST",
url: "/custom_sessions",
data: {
"timezone": "PST"
}
});
这是您的问题:
session('timezone' => $request('timezone'));
抛出语法错误。第一个参数不是 array
,所以使用 =>
会抛出
syntax error, unexpected '=>' (T_DOUBLE_ARROW)
应该是
session()->put("timezone", $request->input("timezone"));
下一个问题是您的函数没有使用 $request
,因此您的 store()
函数应该是:
public function store(Request $request){
session()->put("timezone", $request->input("timezone"));
}
此外,请务必直接引用 Request
:
public function store(\Illuminate\Http\Request $request){ ... }
或将其作为 use
声明添加到 class 的顶部:
use Illuminate\Http\Request;
class CustomSessionsController extends Controller { ... }
当用户第一次访问索引路径上的网站时,我将获取用户的时区。
我在我的 index.blade.html 文件中有一个 ajax 请求。 "/"
我正在将 ajax 路线发布到这条路线“/custom_sessions”。
获取错误
jquery-1.12.4.min.js:4 POST http://127.0.0.1:8000/custom_sessions 500(内部服务器错误)
// route the ajax is hitting
Route::post('/custom_sessions', 'CustomSessionsController@store');
-
// controller
class CustomSessionsController extends Controller
{
public function store()
{
// when i remove this line the ajax request is successful with no errors in the console
session('timezone' => $request('timezone'));
}
}
-
// have this in my head in my html
<meta name="csrf-token" content="{{ csrf_token() }}">
-
// ajax script at the bottom of my index.blade.php file
$.ajax({
headers: {
"X-CSRF-TOKEN": $('meta[name="csrf-token"]').attr('content')
},
type: "POST",
url: "/custom_sessions",
data: {
"timezone": "PST"
}
});
这是您的问题:
session('timezone' => $request('timezone'));
抛出语法错误。第一个参数不是 array
,所以使用 =>
会抛出
syntax error, unexpected '=>' (T_DOUBLE_ARROW)
应该是
session()->put("timezone", $request->input("timezone"));
下一个问题是您的函数没有使用 $request
,因此您的 store()
函数应该是:
public function store(Request $request){
session()->put("timezone", $request->input("timezone"));
}
此外,请务必直接引用 Request
:
public function store(\Illuminate\Http\Request $request){ ... }
或将其作为 use
声明添加到 class 的顶部:
use Illuminate\Http\Request;
class CustomSessionsController extends Controller { ... }