为什么获取响应是一个数字

时间:2017-10-06 21:18:10

标签: javascript json fetch fetch-api

为了清晰起见,我正在重写这个问题。

我有一个从Drupal后端提供JSON的端点。我在网站的客户端有一个javascript应用程序,我试图通过它调用fetch来获取JSON。但是,fetch会返回单个数字3

fetch('http://irdemo.docksal/my-portal-2/load').then(response => {
 return response.json();
}).then(data => {
  console.log(data);
}).catch(err => {
  console.log(err);
});

注销3。以下是网络选项卡中load对象的标头信息。

Request URL:http://irdemo.docksal/my-portal-2/load
Request Method:GET
Status Code:200 OK
Remote Address:192.168.64.100:80
Referrer Policy:no-referrer-when-downgrade
Response Headers
view parsed
HTTP/1.1 200 OK
Server: nginx/1.10.1
Date: Mon, 09 Oct 2017 13:32:41 GMT
Content-Type: application/json
Transfer-Encoding: chunked
Connection: keep-alive
Expires: Sun, 19 Nov 1978 05:00:00 GMT
Cache-Control: no-cache, must-revalidate
X-Content-Type-Options: nosniff
Request Headers
view parsed
GET /my-portal-2/load HTTP/1.1
Host: irdemo.docksal
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6)AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36
Accept: */*
Referer: http://irdemo.docksal/my-portal-2
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.8    

但是,如果我使用superagent库,我可以得到这样的正确数据:

request('http://irdemo.docksal/my-portal-2/load', (err, response) => {
  console.log(response.body);
});

认为我的端点配置错误,毕竟我没有使用JSON.parse来获取响应主体。我不是在寻找如何修复后端应用程序,但我正在尝试理解我应该在端点上寻找什么。

0 个答案:

没有答案