Laravel angularjs支持::阿贾克斯()始终为假 [英] Laravel angularjs Request::ajax() always false
问题描述
我要建应用程序angularjs和laravel 4。 一切都很好,但我现在需要只允许XHR请求。
这是我在我的控制器的开始。 但这一说法始终为false。
如果(!\支持::阿贾克斯())
{
返回响应:: JSON(阵列('叫停'=>支持::阿贾克斯()));
};
在角我使用标准的$ http服务。
angular.module(APP)
.factory(API,($ HTTP,$ Q,应用程序客户机,apiURL) - >
一流的FB
构造函数: - >
this.deferredData = $ q.defer();
信息:(重载) - >
$ HTTP(
方法:搞定
网址:apiURL +游戏/+ +应用程序客户机/信息
).success((RES) - >
dostuff()
)
在做AJAX调用,在 X-要求 - 以
头通常被设置为 XMLHtt prequest
。 Laravel的支持::阿贾克斯()
方法是建立在一个Symfony2的方法,简单地检查这个头的presence之上。
在2012年10月,Angular.js <一href="https://github.com/angular/angular.js/commit/3a75b1124d062f64093a90b26630938558909e8d">removed这个头,因为他们觉得这是很少使用。
由于@Thrustmaster和你自己的评论中提到,您需要设置:
$ httpProvider.defaults.headers.common [X-要求,以] =XMLHtt prequest
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()));
};
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()
)
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.
In October 2012, Angular.js removed this header because they felt that it was rarely used.
As @Thrustmaster and yourself mentioned in the comments, you need to set:
$httpProvider.defaults.headers.common["X-Requested-With"] = "XMLHttpRequest"
这篇关于Laravel angularjs支持::阿贾克斯()始终为假的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!