Nodejs創建自己的server后,我們如果需要從客戶端利用ajax調用別的服務器端的數據API的接口,這時候出現了ajax跨域問題。?
一種是利用在客戶端解決跨域問題?
這種方案大家可以去網上查查?
另一種方案是在服務器端去請求別的服務器,然后將數據再返回客戶端.這里就涉及到了:?
ajax請求,expressjs接收請求,Nodejs發送REST請求。?
我著重寫寫關于這個方案的解決方法:?
首先利用express創建路由,接收客戶端發送的不同請求。?
express路由可以接收get請求和post請求。?
get請求可以去看API,因為平時我們可能對JSON的處理較多,所以用到POST請求較多,我這里主要寫寫post請求。?
客戶端發送請求:?
客戶端代碼:?
Java代碼?
?
- $.ajax({??
- ??
- ???????type:?'POST',??
- ??
- ???????contentType:?'application/json',??
- ??
- ???????url:?'/internaltool/project/peoples',??
- ??
- ???????data:?null,??
- ??
- ???????async:?false,??
- ??
- ???????dataType:?'json',??
- ??
- ???????success:function?(data){??
- ??
- ???????????result?=?data;??
- ??
- ???????},??
- ??
- ???????error:?function?()?{??
- ??
- ???????????alert("Save?error!");??
- ??
- ???????}??
- ??
- ???});??
- ??
- ??
- ??
- ???????$.ajax({??
- ??
- ???????????type:?'POST',??
- ??
- ???????????contentType:?'application/json',??
- ??
- ???????????url:??'/internaltool/project/peopleInfoById',??
- ??
- ???????????data:?'{"id":?"811435467"}',??
- ??
- ???????????async:?false,??
- ??
- ???????????dataType:?'json',??
- ??
- ???????????success:function?(data){??
- ??
- ???????????},??
- ??
- ???????????error:?function?()?{??
- ??
- ???????????????alert("Save?error!");??
- ??
- ???????????}??
- ??
- ???????});??
Nodejs接收客戶端發送的請求,并且Nodejs服務器端發送REST請求別的服務器端取得數據。?
Nodejs服務器端的代碼:?
Java代碼?
?
- var?express?=?require('express'),??
- ????sr??????=?require('./static_require'),??
- ????app?????=?express.createServer();??
- ???
- ????//?linql?2012/08/13?Add???
- ????app.configure(function(){??
- ????????app.use(express.methodOverride());??
- ????????app.use(express.bodyParser());??
- ????????app.use(app.router);??
- ????});??
- ????//?End??
- var?http?=?require('http');??
- ???
- exports.init?=?function(here)?{??
- ????app.get('/*.js',?sr.getHandler({??
- ????????searchPaths:?[here]??
- ????}));??
- ???
- ????app.get('/*',?function(req,?res)?{??
- ????????res.sendfile(req.param(0))??
- ????});??
- ???
- ????//?linql?2012/08/13?Add??
- ????//?這種情況是普通請求,不帶有json數據處理??
- ????app.post('/internaltool/project/peoples',?function(req,?res)?{??
- ????????//?the?post?options??
- ????????var?optionspost?=?{??
- ????????????host?:?'192.168.1.1',??
- ????????????port?:?'8080',??
- ????????????path?:?'/managesystem/Project/personList',??
- ????????????method?:?'POST'??
- ????????};??
- ???
- ????????//?do?the?POST?call??
- ????????//?服務器端發送REST請求??
- ????????var?reqPost?=?http.request(optionspost,?function(resPost)?{??
- ????????????resPost.on('data',?function(d)?{??
- ????????????????res.send(d);??
- ????????????});??
- ????????});??
- ???
- ????????reqPost.end();??
- ???
- ????????reqPost.on('error',?function(e)?{??
- ????????????console.error(e);??
- ????????});??
- ????});??
- ???
- ????app.post('/internaltool/project/peopleInfoById',?function(req,?res)?{??
- ????????//?Request?of?JSON?data??
- ????????//?接收客戶端的JSON數據??
- ????????var?reqJosnData?=?JSON.stringify(req.body);??
- ???
- ????????//?do?a?POST?request??
- ????????//?prepare?the?header??
- ????????var?postheaders?=?{??
- ????????????'Content-Type'?:?'application/json;?charset=UTF-8',??
- ????????????'Content-Length'?:?Buffer.byteLength(reqJosnData,?'utf8')??
- ????????};??
- ???
- ????????//?the?post?options??
- ????????var?optionspost?=?{??
- ????????????host?:?'192.168.1.1',??
- ????????????port?:?'8080',??
- ????????????path?:?'/managesystem/Project/personMessageById',??
- ????????????method?:?'POST',??
- ????????????headers?:?postheaders??
- ????????};??
- ???
- ????????//?do?the?POST?call??
- ????????var?reqPost?=?http.request(optionspost,?function(resPost)?{??
- ???
- ????????????resPost.on('data',?function(d)?{??
- ????????????????res.send(d);??
- ????????????});??
- ????????});??
- ???
- ????????//?write?the?json?data??
- ????????//?發送REST請求時傳入JSON數據??
- ????????reqPost.write(reqJosnData);??
- ????????reqPost.end();??
- ????????reqPost.on('error',?function(e)?{??
- ????????????console.error(e);??
- ????????});??
- ????});??
- ????//?End??
- };??
關于expres.js可以參照:?
http://www.csser.com/board/4f77e6f996ca600f78000936?
Nodejs發送REST請求可以參照:?
http://isolasoftware.it/2012/05/28/call-rest-api-with-node-js/?