get方式請求數(shù)據(jù):
var theurl = "http://c.m.163.com/nc/article/headline/T1348647853363/0-140.html";
myUrl = encodeURIComponent(theurl);
url = "http://127.0.0.1:3000?myUrl=" + myUrl;
$http({
url: url,
method: "get"
}).then(function(res) {
consoe.log(res);
}, function(err) {
console.log(err);
});
通過jsonp請求數(shù)據(jù),通過動態(tài)創(chuàng)建script標簽來訪問服務器,把回調(diào)函數(shù)名作為參數(shù)傳遞給服務器 服務器請求得到數(shù)據(jù)以后,把數(shù)據(jù)放回到回調(diào)函數(shù)中 前端通過回調(diào)函數(shù)的實現(xiàn)部分,得到數(shù)據(jù)。
var script = document.createElement("script");
url = url + "&callback=xxx";
script.src = url;
document.documentElement.appendChild(script);
angular的$http服務中,callback參數(shù)必須寫成JSON_CALLBACK,意思是告訴angular自己去創(chuàng)建一個回調(diào)函數(shù)
var myUrl = "http://c.m.163.com/nc/article/headline/T1348647853363/0-140.html";
$http({
method: "jsonp",
url: "http://localhost:3000?myUrl=" + myUrl + "&callback=JSON_CALLBACK"
})
.then(function(res) {
console.log(res);
},function(err) {
console.log(err);
});
$http的post請求
$http({
method: "post",
url: "http://localhost:3000",
//post請求需要加這個請求頭設置類型
headers: {
"Content-Type": "application/x-www-form-urlencoded"
},
data: {
myUrl: "http://c.m.163.com/nc/article/headline/T1348647853363/0-140.html"
}
})
.then(function(res) {
console.log(res);
},
function(err) {
console.log(err);
});
promise請求
var promise = $http({
method: "post",
url: "http://localhost:3000",
headers: {
"Content-Type": "application/x-www-form-urlencoded"
},
data: {
myUrl: "http://c.m.163.com/nc/article/headline/T1348647853363/0-140.html"}
});
promise.success(function(res) {
console.log(res);
});
promise.error(function(err) {
console.log(err);
});
//或者
promise.then(function(res) {
console.log(res);
}, function(err) {
console.log(err);
})
NodeJS服務器代碼:
- get請求對應的服務器代碼:
var http = require("http");
var url = require("url");
var qs = require("querystring");
http.createServer(function(req, res) {
res.setHeader("Access-Control-Allow-Origin", "*");
// console.log(req.url);
// 對請求對象的url進行解析,拿到查詢參數(shù)字符串
var query = url.parse(req.url).query;
// console.log(query);
//把key=value字符串轉(zhuǎn)變成對象的方式 方便獲取
var queryObj = qs.parse(query);
//用來接收數(shù)據(jù)的變量
var result = "";
// console.log(queryObj.myUrl);
http.get(queryObj.myUrl, function(request) {
request.on("data", function(data) {
result += data;
});
request.on("end", function() {
if(queryObj.callback) {
var fn = queryObj.callback;
var resultStr = JSON.stringify(result);
var str = fn + "(" + resultStr + ")";
res.end(str);
} else
res.end(result);
});
}).on("error", function(err) {
res.end(err);
})
// res.end("hello sb");
})
.listen(3000, function() {
console.log("服務器啟動成功,監(jiān)聽3000...");
});
2.post請求對應的服務器代碼:
var http = require("http");
var url = require("url");
var qs = require("querystring");
http.createServer(function(req, res) {
res.setHeader("Access-Control-Allow-Origin", "*");
//設置編碼格式
req.setEncoding("utf8");
//用來接收數(shù)據(jù)
var postData = "";
//監(jiān)聽:如果前端有數(shù)據(jù)發(fā)送過來
req.addListener("data", function(data) {
postData += data;
});
//前端數(shù)據(jù)傳輸完畢
req.addListener("end", function() {
//把接收到的data數(shù)據(jù)轉(zhuǎn)換成對象方式
var postDataObj = JSON.parse(postData);
var myUrl = postDataObj.myUrl;
//用來接收網(wǎng)易數(shù)據(jù)的變量
var resultData = "";
http.get(myUrl, function(request) {
request.on("data", function(data) {
resultData += data;
});
request.on("end", function() {
res.end(resultData);
})
}).on("error", function(err) {
console.log(err);
});
});
})
.listen(3000, function() {
console.log("3000端口正在監(jiān)聽中...")
});