从 Laravel 中进行异步 AJAX 调用
Making an asynchronous AJAX call from within Laravel
我正在尝试从 Laravel 中的视图中进行异步 jquery/ajax 调用。有一个后台工作任务,由 Redis Queue 处理,然后依次以 key:value 形式存储在 Redis 上。
假设视图是:
项目 > 应用程序 > 视图 > 主要 > search.blade.php
访问此 key:value 对的脚本位于:
项目 > 应用程序 > 视图 > 主要 > getVal.blade.php
我正在通过 search.blade.php 异步调用 getVal.blade.php :
<script>
var userID = {{ isset($userID)?$userID:'0' }};
$.ajax({
type: "POST",
url: 'main/getVal',
data: { id: userId }
}).done(function( msg ) {
alert( msg );
});
</script>
在routes.php中,我将getVal定义为:
Route::get('main/getVal',function() {
return View::make('main.search');
});
我没有看到警告框。我做错了什么?
您的 'main/getVal'
路由在 routes.php 文件中定义以响应 GET 请求,但您的 jQuery 正在执行 AJAX POST 请求。检查您的网络选项卡,我想您从 Laravel 收到了 404 错误。您还可以添加链接到末尾的 .fail()
函数:
var userID = {{ isset($userID)?$userID:'0' }};
$.ajax({
type: "POST",
url: 'main/getVal',
data: { id: userID }
}).done(function( msg ) {
alert( msg );
}).fail(function() {
alert('failed!');
});
解决办法?改为发出 AJAX GET 请求:
var userID = {{ isset($userID)?$userID:'0' }};
$.ajax({
type: "GET",
url: 'main/getVal',
data: { id: userID }
}).done(function( msg ) {
alert( msg );
}).fail(function() {
alert('failed!');
});
或者,更改 Laravel 中的路线以响应 POST 请求:
Route::post('main/getVal',function() {
return View::make('main.search');
});
我发现了以下内容:
- 存在导致错误的错字(userID 与 userId)
- url:'main/getVal' 是一个相对地址,在 Laravel 中我会使用它,因此它适用于任何页面:
url: '{{ url('main/getVal')}}'
- 正如 watcher 所述,您需要处理 POST 请求
适合我的完整代码:
# routes.php
Route::get('main/getVal',function() {
return view('main.search');
});
Route::post('main/getVal',function() {
return 'This is a test';
});
# search.blade.php
<!DOCTYPE html>
<html>
<head>
<title></title>
<script src="//code.jquery.com/jquery-1.11.2.min.js"></script>
<script>
var userID = {{ isset($userID)?$userID:'0' }};
$.ajax({
type: "POST",
url: "{{ url('main/getVal')}}",
data: { id: userID }
}).done(function( msg ) {
alert( msg );
});
</script>
</head>
<body>
</body>
</html>
我正在尝试从 Laravel 中的视图中进行异步 jquery/ajax 调用。有一个后台工作任务,由 Redis Queue 处理,然后依次以 key:value 形式存储在 Redis 上。
假设视图是:
项目 > 应用程序 > 视图 > 主要 > search.blade.php
访问此 key:value 对的脚本位于:
项目 > 应用程序 > 视图 > 主要 > getVal.blade.php
我正在通过 search.blade.php 异步调用 getVal.blade.php :
<script>
var userID = {{ isset($userID)?$userID:'0' }};
$.ajax({
type: "POST",
url: 'main/getVal',
data: { id: userId }
}).done(function( msg ) {
alert( msg );
});
</script>
在routes.php中,我将getVal定义为:
Route::get('main/getVal',function() {
return View::make('main.search');
});
我没有看到警告框。我做错了什么?
您的 'main/getVal'
路由在 routes.php 文件中定义以响应 GET 请求,但您的 jQuery 正在执行 AJAX POST 请求。检查您的网络选项卡,我想您从 Laravel 收到了 404 错误。您还可以添加链接到末尾的 .fail()
函数:
var userID = {{ isset($userID)?$userID:'0' }};
$.ajax({
type: "POST",
url: 'main/getVal',
data: { id: userID }
}).done(function( msg ) {
alert( msg );
}).fail(function() {
alert('failed!');
});
解决办法?改为发出 AJAX GET 请求:
var userID = {{ isset($userID)?$userID:'0' }};
$.ajax({
type: "GET",
url: 'main/getVal',
data: { id: userID }
}).done(function( msg ) {
alert( msg );
}).fail(function() {
alert('failed!');
});
或者,更改 Laravel 中的路线以响应 POST 请求:
Route::post('main/getVal',function() {
return View::make('main.search');
});
我发现了以下内容:
- 存在导致错误的错字(userID 与 userId)
- url:'main/getVal' 是一个相对地址,在 Laravel 中我会使用它,因此它适用于任何页面: url: '{{ url('main/getVal')}}'
- 正如 watcher 所述,您需要处理 POST 请求
适合我的完整代码:
# routes.php
Route::get('main/getVal',function() {
return view('main.search');
});
Route::post('main/getVal',function() {
return 'This is a test';
});
# search.blade.php
<!DOCTYPE html>
<html>
<head>
<title></title>
<script src="//code.jquery.com/jquery-1.11.2.min.js"></script>
<script>
var userID = {{ isset($userID)?$userID:'0' }};
$.ajax({
type: "POST",
url: "{{ url('main/getVal')}}",
data: { id: userID }
}).done(function( msg ) {
alert( msg );
});
</script>
</head>
<body>
</body>
</html>