2016-03-28 59 views
0

我正在爲使用服務器端的nodejs的Android應用程序構建後端,android應用程序向服務器發出POST請求,並向服務器發送所需的參數,服務器負責執行處理併發送響應到請求的用戶作爲SMS.I有一個事務性的SMS網關API,它可以完美地發送SMS作爲來自瀏覽器的GET請求。但是同一個API在發送GET請求到API使用請求模塊節點腳本...在節點JS中發出GET請求的問題

下面給出的是我的代碼,請建議我要去哪裏錯了..

var express = require('express'); 
var request = require("request"); 
var bodyParser = require('body-parser'); 
var app = express(); 
var userName = "myUserName"; 
var passWord = "myPassWord"; 
var senderID = "mySenderID"; 
var content; 
var from_number; 

app.set('port', (process.env.PORT || 5000)); 
app.use(express.static(__dirname + '/public')); 
app.get('/', function(request, response) { 
response.send('We Dont Reply for GET/,make a POST/ instead..'); 
}); 

app.post('/', 
bodyParser.urlencoded({ extended: true }), 
function(req, res) { 
    content = req.body.content; //Parameters passed as POST Request from my Android App 
    from_number = req.body.from_number; 
    console.log('making request to offlinebrowser-web with URL as ' + content + ' and sender as ' + from_number); 
    request("http://offlinebrowser-web.appspot.com/ExtractServlet?url=http://"+content+"&OutputType=1&ExtractorType=1", function(error, response, data) { 
    console.log('backend response : ' + data); //logs to console the valid output of GET Request 
    console.log('initiating response module'); 
    request("**3.*5.*4.1*9/api/v3/sendsms/plain?user="+userName+"&password="+passWord+"sZd5y6AA&sender="+senderID+"&SMSText="+data+"&type=longsms&GSM="+from_number, function(error, response, body) 
    //making a GET request to the above link in browser,perfectly sends an SMS,but here doesnt work! 
    { 
     console.log(body); //this line logs undefined to the console 
    }); 
    res.status(200).end(); 
    }); 
}); 
app.listen(app.get('port'), function() { 
    console.log('Node app is running on port', app.get('port')); 
}); 
+0

您是否在console.log中輸入錯誤或狀態碼?如果是這樣,他們是什麼? – scottjustin5000

+0

正如我在代碼 –

+0

中的評論中提到的,API請求日誌'undefined'的console.log(body)當然,但錯誤或狀態代碼又如何? – scottjustin5000

回答

1

我已經解決了這個問題,只需在API URI前加一個http://