2013-12-13 50 views
-1

你能告訴我如何調用json webservice並獲取輸​​出嗎?我需要一些步驟來完成這個任務。 我剛剛開始使用node.js. 我創建使用命令js文件PROMT我運行該文件使用node.js調用webservice時出錯?

var request = require("request"); 
request.get("http://ergast.com/api/f1/current/last/results.json", function (err, res, body) { 
    if (!err) { 
     var resultsObj = JSON.parse(body); 
     //Just an example of how to access properties: 
     console.log(resultsObj.MRData); 
    } 
}); 


    Microsoft Windows XP [Version 5.1.2600] 
(C) Copyright 1985-2001 Microsoft Corp. 

C:\Documents and Settings\nksharma.Q3TECH>cd.. 

C:\Documents and Settings>cd.. 

C:\>cd node 

C:\node>npm install request 
npm http GET https://registry.npmjs.org/request 
npm http 304 https://registry.npmjs.org/request 
npm http GET https://registry.npmjs.org/json-stringify-safe 
npm http GET https://registry.npmjs.org/qs 
npm http GET https://registry.npmjs.org/forever-agent 
npm http GET https://registry.npmjs.org/node-uuid 
npm http GET https://registry.npmjs.org/tough-cookie 
npm http GET https://registry.npmjs.org/form-data 
npm http GET https://registry.npmjs.org/oauth-sign 
npm http GET https://registry.npmjs.org/http-signature 
npm http GET https://registry.npmjs.org/mime 
npm http GET https://registry.npmjs.org/tunnel-agent 
npm http GET https://registry.npmjs.org/hawk 
npm http GET https://registry.npmjs.org/aws-sign2 
npm http 304 https://registry.npmjs.org/forever-agent 
npm http 304 https://registry.npmjs.org/node-uuid 
npm http 304 https://registry.npmjs.org/tough-cookie 
npm http 304 https://registry.npmjs.org/form-data 
npm http 304 https://registry.npmjs.org/oauth-sign 
npm http 304 https://registry.npmjs.org/http-signature 
npm http 304 https://registry.npmjs.org/mime 
npm http 304 https://registry.npmjs.org/tunnel-agent 
npm http 304 https://registry.npmjs.org/hawk 
npm http 304 https://registry.npmjs.org/aws-sign2 
npm http 304 https://registry.npmjs.org/json-stringify-safe 
npm http 304 https://registry.npmjs.org/qs 
npm http GET https://registry.npmjs.org/combined-stream 
npm http GET https://registry.npmjs.org/async 
npm http GET https://registry.npmjs.org/asn1/0.1.11 
npm http GET https://registry.npmjs.org/assert-plus/0.1.2 
npm http GET https://registry.npmjs.org/ctype/0.5.2 
npm http GET https://registry.npmjs.org/punycode 
npm http GET https://registry.npmjs.org/sntp 
npm http GET https://registry.npmjs.org/boom 
npm http GET https://registry.npmjs.org/cryptiles 
npm http GET https://registry.npmjs.org/hoek 
npm http 304 https://registry.npmjs.org/async 
npm http 304 https://registry.npmjs.org/asn1/0.1.11 
npm http 304 https://registry.npmjs.org/ctype/0.5.2 
npm http 304 https://registry.npmjs.org/assert-plus/0.1.2 
npm http 304 https://registry.npmjs.org/punycode 
npm http 304 https://registry.npmjs.org/sntp 
npm http 304 https://registry.npmjs.org/boom 
npm http 304 https://registry.npmjs.org/cryptiles 
npm http 304 https://registry.npmjs.org/hoek 
npm http 304 https://registry.npmjs.org/combined-stream 
npm http GET https://registry.npmjs.org/delayed-stream/0.0.5 
npm http 304 https://registry.npmjs.org/delayed-stream/0.0.5 
[email protected] node_modules\request 
├── [email protected] 
├── [email protected] 
├── [email protected] 
├── [email protected] 
├── [email protected] 
├── [email protected] 
├── [email protected] 
├── [email protected] 
├── [email protected] ([email protected]) 
├── [email protected] ([email protected], [email protected], [email protected], [email protected]) 
├── [email protected] ([email protected], [email protected], [email protected]) 
└── [email protected] ([email protected], [email protected]) 

C:\node>node b.js 
{ xmlns: 'http://ergast.com/mrd/1.2', 
    series: 'f1', 
    url: 'http://ergast.com/api/f1/current/last/results.json', 
    limit: '30', 
    offset: '0', 
    total: '22', 
    RaceTable: { season: '2013', round: '19', Races: [ [Object] ] } } 

C:\node> 

回答

0

我想你沒有安裝請求模塊,但你要使用它。導航到項目文件夾在您的控制檯,並安裝請求模塊:

npm install request 

然後再次嘗試時,找不到模塊的錯誤應該消失。

+0

請檢查我的更新 – user2648752

+0

仍然我面臨一個問題沒有得到結果 – user2648752

+0

看起來不錯。你看到的是你用console.log(resultsObj.MRData)記錄的結果對象。 ;) – homtg

相關問題