我已经安装了Laravel Homestead,它正常工作,我能够与后端进行通信,并使用API获得响应。现在,我还安装了AngularJS,它似乎也有效,但是行不通的是从Angular应用程序作为前端发布或进入Laravel API。我在Firefox Inspector网络中遇到以下错误:
405 Method Not Allowed
request headers
Host: demo.app:8000
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:37.0) Gecko/20100101 Firefox/37.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Origin: http://frontend.app:8000
Access-Control-Request-Method: POST
Access-Control-Request-Headers: content-type
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache
Response Header
Cache-Control: no-cache
Connection: keep-alive
Content-Type: text/html; charset=UTF-8
Date: Fri, 01 May 2015 02:28:25 GMT
Server: nginx/1.6.2
Set-Cookie: laravel_session=
Transfer-Encoding: chunked
和控制台日志:
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://demo.app:8000/myUrl This can be fixed by moving the resource to the same domain or enabling CORS.
我不确定他们为什么不交流我正确地遵循所有说明,而且他们独立工作(Laravel API,而Angularjs for Frontend)。任何想法和帮助将不胜感激
您需要在/app/filters.php
中添加/替换这些行App::before(function ($request) {
header("Access-Control-Allow-Origin: *");
header('Access-Control-Allow-Credentials: true');
header('Access-Control-Allow-Methods: POST, GET, OPTIONS, PUT, DELETE');
});