Php Can';t使用$request检查请求是否为ajax->;ajax()和Laravel 5.4
我在检查请求是否为ajax时遇到问题。这是我的代码: 路线 控制器Php Can';t使用$request检查请求是否为ajax->;ajax()和Laravel 5.4,php,json,ajax,laravel,response,Php,Json,Ajax,Laravel,Response,我在检查请求是否为ajax时遇到问题。这是我的代码: 路线 控制器 namespace app\Http\Controllers; use Illuminate\Http\Request; class CookiesController extends Controller { public function weUseCookies(Request $request) { if($request->ajax()){ return r
namespace app\Http\Controllers;
use Illuminate\Http\Request;
class CookiesController extends Controller
{
public function weUseCookies(Request $request)
{
if($request->ajax()){
return response()->json(['status' => 'successful']);
}else{
return response()->json(['status' => 'error']);
}
}
}
表单(使用Laravel collective,它会自动创建_令牌)
我试过用另一种方法
$.ajax({
url: url,
type: 'POST',
data: data,
dataType: 'JSON',
success: function (data) {
console.log(data);
}
});
并使用来自的jquery 3.2.1
但它总是从我的控制器返回“错误”
我还尝试使用类似的Request::ajax(),但它跳过if{}并从我的控制器转到else{}选项
我做错了什么
我的代码可以在本地运行,但不能在服务器上运行laravel使用X-request-With-http头来检查传入的请求是否是ajax,您还必须在表单中添加@csrf字段:
$.ajax({
url: url,
type: 'POST',
// add _token field for csrf protection
data: {
_token : ''
},
dataType: 'JSON',
// add x-forwarded-with also add X-CSRF-TOKEN header for csrf protection
headers: {
'X-Requested-With': 'XMLHttpRequest',
'X-CSRF-TOKEN': ..,
}
});
或者您可以使用,因为laravel可以更好地使用它:
axios({
method: 'POST',
url: 'url',
data: {
_token: 'token',
},
responseType: 'json',
headers: {
'X-Requested-With': 'XMLHttpRequest',
'X-CSRF-TOKEN': 'token,
}
}
Request::wantsJson()
它也可以与axios配合使用。您需要在app.js中设置类似于bellow的标题
window.axios = require('axios');
window.axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';
let token = document.head.querySelector('meta[name="csrf-token"]');
if (token) {
window.axios.defaults.headers.common['X-CSRF-TOKEN'] = token.content;
} else {
console.error('CSRF token not found: https://laravel.com/docs/csrf#csrf-x-csrf-token');
}
window.Vue = require('vue');
Request::wantsJson()
它正在使用axios,对我来说记住将CSRF令牌添加到表单中{{csrf_field()}}
如果您还没有设置meta标记,也就是说。它已经添加到我的表单中。如果无法使用普通的$.ajax,我将稍后尝试axios。但我注意到控制器正在接收数据。csrf_字段()在我的表单中。如果我在If($request->ajax())之前添加return response()->json($request),那么数据就在那里(csrf_字段和其他字段)并获得状态200。我现在将尝试使用axios。axios不起作用:(.它发送数据,控制器接收数据,我可以看到数据并使用它,但是($request->ajax()){…}行被“跳过”。Laravel可能是问题吗?我可以决定不使用该行,但现在我想知道->ajax()为什么不起作用。
$.ajax({
url: url,
type: 'POST',
// add _token field for csrf protection
data: {
_token : ''
},
dataType: 'JSON',
// add x-forwarded-with also add X-CSRF-TOKEN header for csrf protection
headers: {
'X-Requested-With': 'XMLHttpRequest',
'X-CSRF-TOKEN': ..,
}
});
axios({
method: 'POST',
url: 'url',
data: {
_token: 'token',
},
responseType: 'json',
headers: {
'X-Requested-With': 'XMLHttpRequest',
'X-CSRF-TOKEN': 'token,
}
}
window.axios = require('axios');
window.axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';
let token = document.head.querySelector('meta[name="csrf-token"]');
if (token) {
window.axios.defaults.headers.common['X-CSRF-TOKEN'] = token.content;
} else {
console.error('CSRF token not found: https://laravel.com/docs/csrf#csrf-x-csrf-token');
}
window.Vue = require('vue');