簡體   English   中英

Fetch 方法在 Node 中不起作用,但在網絡瀏覽器中起作用

[英]Fetch method is not working in Node but in web-browser

我試過使用 unirest 包和我從 Postman 復制的代碼,它可以工作,但是當我嘗試使用 node-fetch 做同樣的事情時,它失敗了。 如果我將此代碼復制到 Chrome 或 Mozilla,它會以某種方式正常工作。 我在這里缺少什么?

  const test = async () => {
  const res = await fetch('http://vhost3.lnu.se:20080/dinner/login', {
    credentials: 'include',
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    body: 'username=zeke&password=coys&submit=login',
    method: 'POST'
  })
  const data = await res
  return data
}

這是節點終端的響應:

Response {
  size: 0,
  timeout: 0,
  [Symbol(Body internals)]: {
    body: PassThrough {
      _readableState: [ReadableState],  
      readable: true,
      _events: [Object: null prototype],
      _eventsCount: 2,
      _maxListeners: undefined,
      _writableState: [WritableState],
      writable: false,
      allowHalfOpen: true,
      _transformState: [Object],
      [Symbol(kCapture)]: false
    },
    disturbed: false,
    error: null
  },
  [Symbol(Response internals)]: {
    url: 'http://vhost3.lnu.se:20080/dinner/login/booking',
    status: 401,
    statusText: 'Unauthorized',
    headers: Headers { [Symbol(map)]: [Object: null prototype] },
    counter: 1
 }
}
  1. 它正在使用您的瀏覽器 cookie。 當您登錄到該服務器時。 它保存了一些 cookie(令牌)。 這樣您就不必再次登錄。 郵遞員使用 chrome cookie。 這就是工作的原因。 試試隱身模式。

  2. 其他原因,您的瀏覽器可能啟用了代理。 比您也必須在節點應用程序中傳遞代理詳細信息。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM