Laravel angularjs Request::ajax() 总是假的 [英] Laravel angularjs Request::ajax() always false
问题描述
我正在使用 angularjs 和 laravel 4 构建应用程序.一切都很好,但我现在只需要允许 XHR 请求.
I'm building application with angularjs and laravel 4. Everything is fine but I need now to allow only XHR requests.
这是我在控制器开头的内容.但这种说法总是错误的.
This is what I have at the beginning of my controller. But this statement is always false.
if (!\Request::ajax())
{
return Response::json(array('halt'=>Request::ajax()));
};
在 angular 中,我使用的是标准的 $http 服务.
In angular I'm using standard $http service.
angular.module('APP')
.factory("API", ($http,$q,appClient,apiURL) ->
class FB
constructor:->
this.deferredData = $q.defer();
info: (reload)->
$http(
method: "get"
url: apiURL+'game/'+appClient+"/info"
).success((res)->
dostuff()
)
推荐答案
在进行 AJAX 调用时,X-Requested-With
标头通常设置为 XMLHttpRequest
.Laravel 的 Request::ajax()
方法建立在 Symfony2 方法之上,该方法只是检查此标头的存在.
When doing AJAX calls, the X-Requested-With
header is often set to XMLHttpRequest
. Laravel's Request::ajax()
method is built on top of a Symfony2 method that simply checks for the presence of this header.
2012 年 10 月,Angular.js 删除这个标头,因为他们觉得它很少使用.
In October 2012, Angular.js removed this header because they felt that it was rarely used.
正如@Thrustmaster 和你自己在评论中提到的,你需要设置:
As @Thrustmaster and yourself mentioned in the comments, you need to set:
$httpProvider.defaults.headers.common["X-Requested-With"] = "XMLHttpRequest"
这篇关于Laravel angularjs Request::ajax() 总是假的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!