我正在使用Angular前端(基于Yeoman)和Symfony 2,FOSRestBundle后端开发应用程序。当我发送PUT请求(或该事件的POST请求)时,它将暂停状态。我正在使用CORS并且所有标头都设置正确。预检OPTIONS请求也具有正确的标头并且处理正确。
[Chrome开发者工具PUT请求] => http://i.stack.imgur.com/lkCtL.png
当我在后端用XDEBUG检查PUT请求时,服务器正确处理请求,服务器向前端发送正确的响应。
[后端的XDEBUG信息] => http://i.stack.imgur.com/G5EBI.png
当我通过设置断点并单步执行代码来跟踪前端的请求时,它永远不会从承诺到达.then
方法。
控制器的代码是:
/**
* Will create a reservation for a single availability (exact + intersect)
*
* @param Object availabilities
*/
$scope.createReservationSingle = function(availabilities)
{
var availability = availabilities[0],
id = availability.id,
parkingspace = availability.parkingspace_id,
reservationstart = moment($scope.request.start).format('YYYY-MM-DD HH:mm:ss'),
reservationend = moment($scope.request.end).format('YYYY-MM-DD HH:mm:ss'),
requeststart = moment(availability.requeststart).format('YYYY-MM-DD HH:mm:ss'),
requestend = moment(availability.requestend).format('YYYY-MM-DD HH:mm:ss'),
user = availability.user_id,
data = { 'guest' : $scope.user.id,
'user': user,
'parkingspace': parkingspace,
'requeststart': requeststart,
'requestend': requestend,
'reservationstart': reservationstart,
'reservationend' : reservationend,
'yield' : $scope.request.points,
'status' : '1'
};
console.log(data);
Reservation.createReservation(id, data).then(function(result) {
console.log(result);
}, function(reason) {
console.log('ReservationsCtrl::createRequest', 'ERROR', reason);
});
};
该服务的代码是:
/**
* Updates a request to a reservation by adding the start- and end datetime, guest_id and points.
*
* @param data
* @returns {promise}
*/
var createReservation = function(id, data)
{
var deferred = $q.defer(),
url = "/api/v1/reservations/"+ parseInt(id) +".json";
$http.put(url, data).success(deferred.resolve).error(deferred.reject);
console.log(deferred.promise);
return deferred.promise;
}
我真的很困惑并且坚持到这里,当我运行(几乎是确切的代码)而没有启用CORS(来自同一个域,因此不需要CORS)并且没有Yeoman它运行正确并且PUT和POST请求是正确的从服务器返回并由前端应用程序正确接收。
有人有建议吗?
更新: 根据@sebbo的建议,我检查了app / dev / dev.log中的日志:
[2014-08-29 08:25:16] event.DEBUG: Listener "FOS\RestBundle\EventListener\ViewResponseListener::onKernelView" stopped propagation of the event "kernel.view". [] []
[2014-08-29 08:25:16] event.DEBUG: Notified event "kernel.response" to listener "Nelmio\CorsBundle\EventListener\CorsListener::onKernelResponse". [] []
[2014-08-29 08:25:16] event.DEBUG: Notified event "kernel.response" to listener "Symfony\Bridge\Monolog\Handler\FirePHPHandler::onKernelResponse". [] []
[2014-08-29 08:25:16] event.DEBUG: Notified event "kernel.response" to listener "Symfony\Bridge\Monolog\Handler\ChromePhpHandler::onKernelResponse". [] []
[2014-08-29 08:25:16] event.DEBUG: Notified event "kernel.response" to listener "FOS\RestBundle\EventListener\AllowedMethodsListener::onKernelResponse". [] []
[2014-08-29 08:25:16] event.DEBUG: Notified event "kernel.response" to listener "Symfony\Component\HttpKernel\EventListener\ResponseListener::onKernelResponse". [] []
[2014-08-29 08:25:16] event.DEBUG: Notified event "kernel.response" to listener "Symfony\Component\Security\Http\RememberMe\ResponseListener::onKernelResponse". [] []
[2014-08-29 08:25:16] event.DEBUG: Notified event "kernel.response" to listener "Sensio\Bundle\FrameworkExtraBundle\EventListener\CacheListener::onKernelResponse". [] []
[2014-08-29 08:25:16] event.DEBUG: Notified event "kernel.response" to listener "Symfony\Component\HttpKernel\EventListener\ProfilerListener::onKernelResponse". [] []
[2014-08-29 08:25:17] event.DEBUG: Notified event "kernel.response" to listener "Symfony\Bundle\WebProfilerBundle\EventListener\WebDebugToolbarListener::onKernelResponse". [] []
[2014-08-29 08:25:17] event.DEBUG: Notified event "kernel.response" to listener "Symfony\Component\HttpKernel\EventListener\StreamedResponseListener::onKernelResponse". [] []
[2014-08-29 08:25:17] event.DEBUG: Notified event "kernel.terminate" to listener "Symfony\Bundle\SwiftmailerBundle\EventListener\EmailSenderListener::onTerminate". [] []
这里并不特别,我猜......:'(
为了确保我还检查了Apache访问和错误日志。
Apache access-log:
127.0.0.1 - - [29/Aug/2014:08:24:37 +0200] "GET /oauth/v2/token?client_id=3_2r03j9282aecks0c488owcko4w8gkc8w800kog4w8gw0g8ck0w&client_secret=1elz2h57nyiscwg04k8gccsskscsc4g88sccosk0sc0ogwg48s&grant_type=password&username=ashtonwenzel&password=ashtonwenzel HTTP/1.1" 200 265
127.0.0.1 - - [29/Aug/2014:08:24:39 +0200] "GET /api/v1/users/current.json?access_token=MjgyZDBiZjgyNDBiZDNjYTJjNzkxYzJiZjk3OWZkOWU5ZGUzODYzYmVmMDYxMWRmOTg3ODdhOWE4MTQwMzcwYg&XDEBUG_SESSION_START=PHPSTORM HTTP/1.1" 200 643
127.0.0.1 - - [29/Aug/2014:08:24:58 +0200] "GET /api/v1/users/5/availabilities/search.json?start=2014-08-29%2008:30&end=2014-08-29%2008:45&access_token=MjgyZDBiZjgyNDBiZDNjYTJjNzkxYzJiZjk3OWZkOWU5ZGUzODYzYmVmMDYxMWRmOTg3ODdhOWE4MTQwMzcwYg&XDEBUG_SESSION_START=PHPSTORM HTTP/1.1" 200 181
127.0.0.1 - - [29/Aug/2014:08:25:12 +0200] "OPTIONS /api/v1/reservations/9040.json?access_token=MjgyZDBiZjgyNDBiZDNjYTJjNzkxYzJiZjk3OWZkOWU5ZGUzODYzYmVmMDYxMWRmOTg3ODdhOWE4MTQwMzcwYg&XDEBUG_SESSION_START=PHPSTORM HTTP/1.1" 200 -
127.0.0.1 - - [29/Aug/2014:08:25:12 +0200] "PUT /api/v1/reservations/9040.json?access_token=MjgyZDBiZjgyNDBiZDNjYTJjNzkxYzJiZjk3OWZkOWU5ZGUzODYzYmVmMDYxMWRmOTg3ODdhOWE4MTQwMzcwYg&XDEBUG_SESSION_START=PHPSTORM HTTP/1.1" 204 360374
Apache错误日志:
我只使用PHP中的error_log
函数手动添加了一些东西。
仍然完全陷入困境,对于读这篇文章的人来说,我真的,真的需要一些关于在哪里寻找的建议......:)
更新2:
以下是OPTIONS和PUT请求的详细信息。
OPTIONS:
Remote Address:127.0.0.1:80
Request URL:http://develop.prs.nl/api/v1/reservations/6514.json?access_token=ZDQxODJkNDFmZTNhNTBjMTQ0OTE1MjlmZTcyOWMzZGUxNjViNDA4NjZlM2QxMThmNDdiMzIwODJiMTQ0MDU1Zg&XDEBUG_SESSION_START=PHPSTORM
Request Method:OPTIONS
Status Code:200 OK
Request Headersview source
Accept:*/*
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8,de;q=0.6,nl;q=0.4,sq;q=0.2
Access-Control-Request-Headers:accept, content-type
Access-Control-Request-Method:PUT
Connection:keep-alive
Host:develop.prs.nl
Origin:http://frontend.prs.nl
Referer:http://frontend.prs.nl/
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.94 Safari/537.36
Query String Parametersview sourceview URL encoded
access_token:ZDQxODJkNDFmZTNhNTBjMTQ0OTE1MjlmZTcyOWMzZGUxNjViNDA4NjZlM2QxMThmNDdiMzIwODJiMTQ0MDU1Zg
XDEBUG_SESSION_START:PHPSTORM
Response Headersview source
Access-Control-Allow-Headers:origin, content-type, accept
Access-Control-Allow-Methods:POST, PUT, GET, DELETE, PATCH, OPTIONS
Access-Control-Allow-Origin:http://frontend.prs.nl
Access-Control-Max-Age:3600
Cache-Control:no-cache
Connection:Keep-Alive
Content-Type:text/html; charset=UTF-8
Date:Fri, 29 Aug 2014 13:50:58 GMT
Keep-Alive:timeout=5, max=100
Server:Apache/2.2.26 (Unix) DAV/2 PHP/5.4.24 mod_ssl/2.2.26 OpenSSL/0.9.8y
Set-Cookie:XDEBUG_SESSION=PHPSTORM; expires=Fri, 29-Aug-2014 14:50:58 GMT; path=/
Transfer-Encoding:chunked
X-Debug-Token:ac1b50
X-Powered-By:PHP/5.4.24
PUT:
Request URL:http://develop.prs.nl/api/v1/reservations/6514.json?access_token=ZDQxODJkNDFmZTNhNTBjMTQ0OTE1MjlmZTcyOWMzZGUxNjViNDA4NjZlM2QxMThmNDdiMzIwODJiMTQ0MDU1Zg&XDEBUG_SESSION_START=PHPSTORM
Request Headers
Provisional headers are shown
Accept:application/json, text/plain, */*
Content-Type:application/json;charset=UTF-8
Origin:http://frontend.prs.nl
Referer:http://frontend.prs.nl/
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.94 Safari/537.36
X-DevTools-Emulate-Network-Conditions-Client-Id:DC58FE79-4B4C-689A-A632-B5886576FACC
Query String Parametersview sourceview URL encoded
access_token:ZDQxODJkNDFmZTNhNTBjMTQ0OTE1MjlmZTcyOWMzZGUxNjViNDA4NjZlM2QxMThmNDdiMzIwODJiMTQ0MDU1Zg
XDEBUG_SESSION_START:PHPSTORM
Request Payloadview source
{guest:5, user:41, parkingspace:42, requeststart:2014-08-29 14:30:00, requestend:2014-08-29 20:00:00,…}
guest: 5
parkingspace: "42"
requestend: "2014-08-29 20:00:00"
requeststart: "2014-08-29 14:30:00"
reservationend: "2014-08-29 16:15:00"
reservationstart: "2014-08-29 16:00:00"
status: "1"
user: "41"
yield: 1
对我来说,在对CORS等进行彻底调查之后,请求上的标题看起来很好。但是......仍然完全迷失了。
今晚当我有更多时间时,我会用纯jQuery(juk!)创建一个PUT请求,以检查问题是存在于前端(或Angular)还是后端(尽管Symfony) / Apache日志告诉我,否则。)
如果有人有任何其他建议,欢迎他们!
更新II:
仍然无法弄清楚出了什么问题。我将前端放在与后端完全相同的域上,然后它就可以了。所以我怀疑与CORS有关...... :((见评论)
也许对NelmioCorsBundle或FOSRestBundle有经验的人可以看一下我粘贴的配置:
fos_rest:
param_fetcher_listener: true
view:
view_response_listener: 'force'
formats:
xml: true
json: true
templating_formats:
html: true
format_listener:
rules:
- { path: ^/, priorities: [ html, json, xml ], fallback_format: ~, prefer_extension: true }
exception:
codes:
'Symfony\Component\Routing\Exception\ResourceNotFoundException': 404
'Doctrine\ORM\OptimisticLockException': HTTP_CONFLICT
messages:
'Symfony\Component\Routing\Exception\ResourceNotFoundException': true
allowed_methods_listener: true
access_denied_listener:
json: true
body_listener: true
disable_csrf_role: ROLE_API
nelmio_cors:
paths:
'^/':
allow_origin: ['*']
allow_headers: ['X-Requested-With', 'Origin', 'Content-Type', 'Accept']
allow_methods: ['POST', 'PUT', 'GET', 'DELETE', 'PATCH', 'OPTIONS']
max_age: 3600
在此之后,OPTIONS请求的标题是:
Remote Address:127.0.0.1:80
Request URL:http://develop.prs.nl/api/v1/reservations/12649.json?access_token=MDNmMjM5OWQzOGZmODMzYjk3MWRmNWRhNDBiNTMxMjRiOTI3YzNkYTU2MTJmMTcyMDI5MTZkZjBmOGM3MTVkMg&XDEBUG_SESSION_START=PHPSTORM
Request Method:OPTIONS
Status Code:200 OK
Request Headersview source
Accept:*/*
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8,de;q=0.6,nl;q=0.4,sq;q=0.2
Access-Control-Request-Headers:accept, content-type
Access-Control-Request-Method:PUT
Connection:keep-alive
Host:develop.prs.nl
Origin:http://localhost:9001
Referer:http://localhost:9001/
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.94 Safari/537.36
Query String Parametersview sourceview URL encoded
access_token:MDNmMjM5OWQzOGZmODMzYjk3MWRmNWRhNDBiNTMxMjRiOTI3YzNkYTU2MTJmMTcyMDI5MTZkZjBmOGM3MTVkMg
XDEBUG_SESSION_START:PHPSTORM
Response Headersview source
Access-Control-Allow-Headers:x-requested-with, origin, content-type, accept
Access-Control-Allow-Methods:POST, PUT, GET, DELETE, PATCH, OPTIONS
Access-Control-Allow-Origin:http://localhost:9001
Access-Control-Max-Age:3600
Cache-Control:no-cache
Connection:Keep-Alive
Content-Type:text/html; charset=UTF-8
Date:Tue, 02 Sep 2014 19:24:53 GMT
Keep-Alive:timeout=5, max=100
Server:Apache/2.2.26 (Unix) DAV/2 PHP/5.4.24 mod_ssl/2.2.26 OpenSSL/0.9.8y
Set-Cookie:XDEBUG_SESSION=PHPSTORM; expires=Tue, 02-Sep-2014 20:24:53 GMT; path=/
Transfer-Encoding:chunked
X-Debug-Token:83c356
X-Powered-By:PHP/5.4.24
我仍然没有任何线索...:'(
更新III:
我刚尝试过Curl的请求。这是有效的,这实际上并不是一个好消息,我想我还是不知道为什么浏览器(Chrome和Firefox)的请求一直处于待定状态。
我做的卷曲请求:
curl 'http://develop.prs.nl/api/v1/reservations/20884.json?access_token=MDNmMjM5OWQzOGZmODMzYjk3MWRmNWRhNDBiNTMxMjRiOTI3YzNkYTU2MTJmMTcyMDI5MTZkZjBmOGM3MTVkMg&XDEBUG_SESSION_START=PHPSTORM' -X PUT -H 'Accept: application/json, text/plain, */*' -H 'Referer: http://develop.prs.nl:9001/' -H 'Origin: http://develop.prs.nl:9001' -H 'X-DevTools-Emulate-Network-Conditions-Client-Id: 03571AFA-71C5-867C-1258-3F1D8EC4F8EF' -H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.94 Safari/537.36' -H 'Content-Type: application/json;charset=UTF-8' --data-binary '{"guest":5,"user":"129","parkingspace":"156","requeststart":"2014-09-02 16:00:00","requestend":"2014-12-31 21:00:00","reservationstart":"2014-09-02 21:30:00","reservationend":"2014-09-02 21:45:00","yield":1,"status":"1"}' --compressed -vvv > curl_log.txt
屏幕上的Curl reuqets输出:
* Adding handle: conn: 0x7faca300c400
* Adding handle: send: 0
* Adding handle: recv: 0
* Curl_addHandleToPipeline: length: 1
* - Conn 0 (0x7faca300c400) send_pipe: 1, recv_pipe: 0
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0* About to connect() to develop.prs.nl port 80 (#0)
* Trying 127.0.0.1...
* Connected to develop.prs.nl (127.0.0.1) port 80 (#0)
> PUT /api/v1/reservations/20884.json?access_token=MDNmMjM5OWQzOGZmODMzYjk3MWRmNWRhNDBiNTMxMjRiOTI3YzNkYTU2MTJmMTcyMDI5MTZkZjBmOGM3MTVkMg&XDEBUG_SESSION_START=PHPSTORM HTTP/1.1
> Host: develop.prs.nl
> Accept-Encoding: deflate, gzip
> Accept: application/json, text/plain, */*
> Referer: http://develop.prs.nl:9001/
> Origin: http://develop.prs.nl:9001
> X-DevTools-Emulate-Network-Conditions-Client-Id: 03571AFA-71C5-867C-1258-3F1D8EC4F8EF
> User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.94 Safari/537.36
> Content-Type: application/json;charset=UTF-8
> Content-Length: 220
>
} [data not shown]
* upload completely sent off: 220 out of 220 bytes
100 220 0 0 100 220 0 54 0:00:04 0:00:04 --:--:-- 54< HTTP/1.1 200 OK
< Date: Tue, 02 Sep 2014 19:34:44 GMT
* Server Apache/2.2.26 (Unix) DAV/2 PHP/5.4.24 mod_ssl/2.2.26 OpenSSL/0.9.8y is not blacklisted
< Server: Apache/2.2.26 (Unix) DAV/2 PHP/5.4.24 mod_ssl/2.2.26 OpenSSL/0.9.8y
< X-Powered-By: PHP/5.4.24
< Set-Cookie: XDEBUG_SESSION=PHPSTORM; expires=Tue, 02-Sep-2014 20:34:44 GMT; path=/
< Cache-Control: no-cache
< Access-Control-Allow-Origin: http://develop.prs.nl:9001
< X-ChromeLogger-Data: eyJ2ZXJzaW9uIjoiNC4wIiwiY29sdW1ucyI6WyJsYWJlbCIsImxvZyIsImJhY2t0cmFjZSIsInR5cGUiXSwicm93cyI6W1sicmVxdWVzdCIsIk1hdGNoZWQgcm91dGUgXCJhcGlfMV9wdXRfcmVzZXJ2YXRpb25cIiAocGFyYW1ldGVyczogXCJfY29udHJvbGxlclwiOiBcIlR3b3R3b3Zpc2lvblxcUHJzQnVuZGxlXFxDb250cm9sbGVyXFxSZXNlcnZhdGlvbkNvbnRyb2xsZXI6OnB1dFJlc2VydmF0aW9uQWN0aW9uXCIsIFwiX2Zvcm1hdFwiOiBcImpzb25cIiwgXCJpZFwiOiBcIjIwODg0XCIsIFwiX3JvdXRlXCI6IFwiYXBpXzFfcHV0X3Jlc2VydmF0aW9uXCIpIiwidW5rbm93biIsImluZm8iXV0sInJlcXVlc3RfdXJpIjoiXC9hcGlcL3YxXC9yZXNlcnZhdGlvbnNcLzIwODg0Lmpzb24/YWNjZXNzX3Rva2VuPU1ETm1Nak01T1dRek9HWm1PRE16WWprM01XUm1OV1JoTkRCaU5UTXhNalJpT1RJM1l6TmtZVFUyTVRKbU1UY3lNREk1TVRaa1pqQm1PR00zTVRWa01nJlhERUJVR19TRVNTSU9OX1NUQVJUPVBIUFNUT1JNIn0=
< Allow: GET, PUT, PATCH, DELETE
< X-Debug-Token: a4f9cf
< Transfer-Encoding: chunked
< Content-Type: application/json
<
{ [data not shown]
100 352k 0 351k 100 220 87534 53 0:00:04 0:00:04 --:--:-- 87555
* Connection #0 to host develop.prs.nl left intact
我输出结果的.txt文件的输出是正确的。当我检查数据库时,所有数据都根据我的期望进行了更改。
目前结论,Curl工作正常,在飞行OPTIONS请求后,Chrome和Firefox仍然在PUT请求中处于暂挂状态。真的,真的,真的失去了......
更新IV:
终于解决了!而且f&amp;%*简单如何。骑自行车的时候想到了Nelmio Cors Budnle的配置,当我回到家时,我做了一个小小的改动:
nelmio_cors:
paths:
'^/':
allow_credentials: true
allow_origin: ['*']
allow_headers: ['X-Requested-With', 'X-Prototype-Version', 'Cache-Control', 'Pragma', 'Origin', 'Content-Type', 'Accept']
allow_methods: ['POST', 'PUT', 'GET', 'DELETE', 'PATCH', 'OPTIONS']
expose_headers: ['Access-Control-Allow-Origin']
max_age: 3600
而且,它有效!因此,在Nelmio Cors Bundle配置中添加公开标题部分就可以了。 LOL
比尔斯!!! :)