第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

執(zhí)行 http 請(qǐng)求時(shí)出錯(cuò):未定義:1 <head><title>Document Moved

執(zhí)行 http 請(qǐng)求時(shí)出錯(cuò):未定義:1 <head><title>Document Moved

HUWWW 2022-07-15 09:41:34
在節(jié)點(diǎn)中執(zhí)行 http 請(qǐng)求時(shí)出錯(cuò)。當(dāng)我使用如下代碼時(shí),我得到了錯(cuò)誤。我嘗試了以下代碼:const http = require('http')class postalData { async getPostData(pinCode){  let result = new Promise((resolve, reject) => {    http.get('http://postalpincode.in/api/pincode/' + pinCode,(res)=>{      let data = '';      // A chunk of data has been recieved.      res.on('data', (chunk) => {        data += chunk;      });      // The whole response has been received. Print out the result.      res.on('end', () => {        resolve(JSON.parse(data).explanation);      });    }).on("error", (err) => {      reject(err.message);    });    })  const newLocal = await result  return newLocal }}const getData = new postalData()console.log('Post data : ', getData.getPostData(400705))在節(jié)點(diǎn)中執(zhí)行 http 請(qǐng)求時(shí)出現(xiàn)以下錯(cuò)誤。當(dāng)我使用如下代碼時(shí),我得到了錯(cuò)誤。如下所示:undefined:1<head><title>Document Moved</title></head>^SyntaxError: Unexpected token < in JSON at position 0    at JSON.parse (<anonymous>)    at IncomingMessage.<anonymous> (/home/rahul/indian-post/a.js:23:22)    at IncomingMessage.emit (events.js:327:22)    at endReadableNT (_stream_readable.js:1201:12)    at processTicksAndRejections (internal/process/task_queues.js:84:21)我的預(yù)期輸出:{   "Message":"Number of Post office(s) found: 1",   "Status":"Success",   "PostOffice":[      {         "Name":"Sanpada",         "Description":"",         "BranchType":"Sub Post Office",         "DeliveryStatus":"Delivery",         "Taluk":"NA",         "Circle":"NA",         "District":"Thane",         "Division":"New Mumbai",         "Region":"Mumbai",         "State":"Maharashtra",         "Country":"India"      }   ]}
查看完整描述

2 回答

?
MM們

TA貢獻(xiàn)1886條經(jīng)驗(yàn) 獲得超2個(gè)贊

該請(qǐng)求http://postalpincode.in/api/pincode/400705會(huì)產(chǎn)生一個(gè)帶有標(biāo)頭的301 Moved Permanently響應(yīng),該標(biāo)頭Location: http://www.postalpincode.in/api/pincode/400705告訴您資源在 可用http://www.postalpincode.in/api/pincode/400705

如果您http://postalpincode.in/api/pincode/400705在瀏覽器中打開(kāi),它將自動(dòng)處理該301響應(yīng)并打開(kāi)該給定位置。

node的http模塊不會(huì)這樣做,它會(huì)為您提供實(shí)際的響應(yīng)(這是http模塊的預(yù)期行為)

如果您想使用該模塊,那么您需要編寫(xiě)自己的邏輯來(lái)遵循重定向或使用像requestwich 這樣的庫(kù)提供了遵循這些重定向的選項(xiàng)。

您需要檢查response.statusCode是否是301302,如果是,則需要使用 獲取Location標(biāo)頭response.getHeader(name),然后重新發(fā)出請(qǐng)求,對(duì)于新的響應(yīng),您需要執(zhí)行相同的操作,以防發(fā)生另一個(gè)重定向。


查看完整回答
反對(duì) 回復(fù) 2022-07-15
?
拉莫斯之舞

TA貢獻(xiàn)1820條經(jīng)驗(yàn) 獲得超10個(gè)贊

您正在嘗試使用普通的 http 調(diào)用,但服務(wù)器正在響應(yīng)redirect(Document Moved)。節(jié)點(diǎn)http不會(huì)跟隨重定向,因此它會(huì)響應(yīng)重定向的 HTML。

您可以使用requestpackage來(lái)跟蹤重定向,或者使用更簡(jiǎn)單的包,例如follow-redirectshttp它們?yōu)楦S重定向的節(jié)點(diǎn)提供了一個(gè)替代品。

編輯:如果您不想使用外部庫(kù),只需檢查響應(yīng)標(biāo)頭和 statusCode,如下所示request

if (response.statusCode >= 300 && response.statusCode < 400 && response.headers['location']) { /* retry the http request at the location */ }



查看完整回答
反對(duì) 回復(fù) 2022-07-15
  • 2 回答
  • 0 關(guān)注
  • 307 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)