同构获取不适用于外部请求

Isomorphic fetch not working for external requests?

本文关键字:外部 请求 适用于 不适用 获取 同构      更新时间:2023-09-26

EDIT:我实际上想了解为什么响应不包含我请求的数据,以及这是由于某些库丢失还是由于我的fetchUrl变量的格式

嗨,我正在尝试使用同构的获取方法来进行ajax请求,并且正在努力从外部API获取数据。

这不应该奏效吗?如果不行,我会错过什么?

require('es6-promise').polyfill();
require('isomorphic-fetch');          
router.get('/weather', function(req, res){
          var fetchUrl = 'http://api.wunderground.com/api/xyz-token/conditions/q/CA/San_Francisco.json';
          fetch(fetchUrl, {
            method: "GET"
          })
            .then(function(response){
              if (response.status >= 400) {
                throw new Error("Bad request response from server");
              }
              console.log(response);
              res.send(response);
            });
        });

我的回复看起来像:

{"url":"http://api.wunderground.com/api/xyz-token/conditions/q/CA/San_Francisco.json","status":200,"statusText":"OK","headers":{"_headers":{(CentOS)"],"访问控制允许来源":["*"],"访问控制允许凭据":["true"]、"x-creationtime":["0.140"]、"contentencoding":[,2016年5月3日10:47:33 GMT"],"内容类型":["application/json;charset=UTF-8"],"内容长度":["1043"],2016年5月3日10:48:58 GMT"],"缓存控制":["最大年龄=0,无缓存"],"pragma":["无缓存"],"日期":["2016年5月3日星期二10:48:58GMT"],"connection":["close"]}}},"ok":true,"body":{"_opts":{},"_chunkSize":16384,"_readableState":{"objectMode":false,"highWaterMark":16384%,"buffer":[],"length":0,"pipes":null,"pipesCount":0、"flow":null、"endEmitted":false、"reading":false"readableListening:false,"defaultEncoding":"utf8","ranOut":false,"awaitDrain":0,"readingMore":false,"decoder":null,"encoding":null},"readable":true,"domain":null","_events":{"end":[null,null]},"_eeventsCount":7,"_writebleState":{false,"bufferProcessing":false,"writelen":1043,"bufferedRequest":null,"lastBufferedRequest":null、"pendingcb":1、"prefished":false,"errorEmitted":false}、"writible":true、"allowHalfOpen":true,"_transformState":{"needTransform":false、"transforming":true、"writechunk":{"type":"Buffer"、"data":[31139,8,0,0,0,0,3165,86,91143218,56,20126158249,21,9114,52,72,85133102,52218213118232106,41157125139,60142,19,44146,56107,59195208138255199137,67194165157,74203,1124825120620559,15191920253126139144,37168,441211316953,71245250133,10201120,97205,45215246172129134,20,2115228201138138,23,70192208218,40,85206,29103183219217187170136169,72,513111155240220217,81172,54,84,58,56184100,78236212174246,70229,89,19,42,1182130148109,54194139152,41200167,17,15144195237205,1190,6,55,221691321160133138248179132196,88213,691221215205205141197114156210118,81137172,43137,65,64105239,94171180,46,38,2184220,48,34157,93229,59,25,79121228,5238235212181,203,34133130192,91,49149233,45,61,53101163117183157198,32,99197246231,91182192236,80219198,76150,5222,71,25,39,39245,38,85166107,92225,2,61,8,92,16,38,31160187,69147129,48181,63103,27,70,42172116113173101189142,10156215,32206,88194,69193176,9,325,40161,35173,87,。。。。

Response是一个复杂的对象,它不仅包含有效负载,还包含许多元数据,如http状态和标头。

为了提取实际数据,您可能需要调用以下方法之一:

response.json() // if the response contains json data

response.text() // if there is a plain text

你可以进一步调查MDN。

更新考虑到这两个方法都返回Promise,因此您可以将它们集成到Promise链中:

require('es6-promise').polyfill();
require('isomorphic-fetch');
var express = require('express');
var app = express();
app.get('/weather', function(req, res){
  var fetchUrl = 'http://api.wunderground.com/api/xyz-token/conditions/q/CA/San_Francisco.json';
  fetch(fetchUrl)
    .then(function(response){
      if (response.status >= 400) {
        throw new Error("Bad request response from server");
      }
      return response.json();
    })
    .then(function (json) {
      console.log(json);
      res.json(json);
    });
});
app.listen(8080);

所以我想我已经解决了我的问题。

我需要正确地处理响应对象,但我不明白正确的方法。

我正在请求一个json对象,但不能使用response.json()来获取数据?

工作原理:

      ...
      return response.text();
    }).then(function(body) {
      res.send(body);
      ...

我甚至不明白为什么会这样。响应对象肯定只是一个我可以像其他对象一样钻取的对象吗?有什么区别?