2015-11-07 63 views
2

我试图让Chrome记录器在针对PHP后端运行的Angular应用程序中工作,但由于某些原因,当由Angular访问API时,X-ChromeLogger-Data标题似乎不会通过应用程序。响应标题随角落消失

如果我直接打开API的接入点或者使用jQuery.get()请求打开它,即使我从另一个域发出ajax请求,一切正常。即使在Angular应用程序使用时,该API也能正常工作。只是一个标题在一路上消失了。它甚至不会出现在Chrome的控制台中。

什么可能导致标题消失?

下面是与jQuery.get()

Request: 
GET /?action=load HTTP/1.1 
Host: - 
Connection: keep-alive 
Accept: application/json, text/javascript, */*; q=0.01 
Origin: - 
X-FirePHP-Version: 0.0.6 
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.80 Safari/537.36 
Referer: - 
Accept-Encoding: gzip, deflate, sdch 
Accept-Language: fi-FI,fi;q=0.8,en-US;q=0.6,en;q=0.4,sv;q=0.2 

Response: 
HTTP/1.1 200 OK 
Date: Sat, 07 Nov 2015 10:41:22 GMT 
Server: Apache/2.4.12 (Ubuntu) 
X-Powered-By: PHP/5.6.11-1ubuntu3.1 
Access-Control-Allow-Origin: * 
Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, DELETE 
Access-Control-Allow-Headers: Content-Type, Depth, User-Agent, X-File-Size, X-Requested-With, If-Modified-Since, X-File-Name, Cache-Control, X-ChromeLogger-Data 
X-ChromeLogger-Data: eyJ2ZXJzaW9uIjoiNC4wIiwiY29sdW1ucyI6WyJsYWJlbCIsImxvZyIsImJhY2t0cmFjZSIsInR5cGUiXSwicm93cyI6W1siQVBJIiwiQVBJIHJlYWNoZWQiLCJ1bmtub3duIiwid2FybiJdXSwicmVxdWVzdF91cmkiOiJcLz9hY3Rpb249bG9hZCJ9 
Content-Length: 15 
Keep-Alive: timeout=5, max=100 
Connection: Keep-Alive 
Content-Type: text/html; charset=UTF-8 

提出的要求,下面是一个从角应用:

Request: 
GET /?action=load HTTP/1.1 
Host: - 
Connection: keep-alive 
Accept: application/json, text/plain, */* 
Origin: - 
X-FirePHP-Version: 0.0.6 
User-Agent: Mozilla/5.0 (Linux; U; Android 4.0; en-us; GT-I9300 Build/IMM76D) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30 
Referer: - 
Accept-Encoding: gzip, deflate, sdch 
Accept-Language: fi-FI,fi;q=0.8,en-US;q=0.6,en;q=0.4,sv;q=0.2 

Response: 
HTTP/1.1 200 OK 
Date: Sat, 07 Nov 2015 10:34:33 GMT 
Server: Apache/2.4.12 (Ubuntu) 
X-Powered-By: PHP/5.6.11-1ubuntu3.1 
Access-Control-Allow-Origin: * 
Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, DELETE 
Access-Control-Allow-Headers: Content-Type, Depth, User-Agent, X-File-Size, X-Requested-With, If-Modified-Since, X-File-Name, Cache-Control, X-ChromeLogger-Data 
Content-Length: 15 
Keep-Alive: timeout=5, max=100 
Connection: Keep-Alive 
Content-Type: text/html; charset=UTF-8 

用来做上述要求的角度代码:

$http.get(endpoint, { params : { action : 'load' } }).then(
    function(response) { 
     console.log(response); 
    }, 
    function() { 
     console.log('fail'); 
    } 
); 
+0

你能展示你的角码吗? –

+0

@OlaviSau当然,将其添加到问题中。 – Schlaus

+1

如果我理解正确的话,当你只使用角度'$ http'时,会发生这种情况,并且jQuery或其他任何请求都可以正常工作吗? – Dvir

回答

1

后Dvir的提示,以进一步检查请求之间的差异(借助援助Fiddler)我终于找到了解决问题的经理。我开启了运行Chrome的移动设备模拟器的Angular应用程序,结果发现Monolog ChromePHPHandler需要文本“Chrome”出现在User-Agent标题中,而不是在模拟器打开时显示。