2016-07-17 3 views
1

今私はこのような質問があります: 私はnode.jsでサーバーを作成しました。サーバーはajaxリクエストを受け取りました。 .jsは別のサーバーに投稿要求を送信します。今、別のサーバーからデータを取得しました。主な質問は、データをajaxに戻す方法です。多くの方法を試しましたが、うまくいきません。についてnode.jsはajax呼び出し関数にデータを返します

誰か助けてもらえますか?ここ

私のコード

==== AJAXリクエスト

$.ajax({ 
     type: "POST", 
     url: 'http://localhost:8888', // 这里要改成服务器的地址 
     data: userData, 
     success: function (data) { 
      console.log(data); 
     } 
    }) 

====

http.createServer(function (req, res) { 
    if (req.url == '/') { 
     var data = ''; 
     var imdata; 
     util.log(util.inspect(req)); 
     util.log('Request recieved: \nmethod: ' + req.method + '\nurl: ' + req.url); 
     req.on('data', function (chunk) { 
      imdata = querystring.parse(data += chunk);//转成对象的格式 
     }) 
     req.on('end', function() { 
      var myIm = new ServerApi('e782429e48cb99f44b9c5effe414ac72', 'b88b9f2a2f74'); 
      myIm.createUserId(imdata, function (err, data) { 
//createUesrId is a api to deal with post request 
       console.log(data);//the data have received from another server,and now i do not know how to return the data to ajax success function 

      }) 
     }) 

====ユーザーIDを作成するためのAPIです投稿後の設定で

ServerApi.prototype.postDataHttps = function (url, data, callback) { 
    this.checkSumBuilder(); 

    var urlObj = urlParser.parse(url); 
    var httpHeader = { 
     'AppKey': this.AppKey, 
     'Nonce': this.Nonce, 
     'CurTime': this.CurTime, 
     'CheckSum': this.CheckSum, 
     'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8', 
     'Content-Length': Buffer.byteLength(data) 
    }; 
    var options = { 
     hostname: urlObj.hostname, 
     port: 80, 
     path: urlObj.path, 
     method: 'POST', 
     headers: httpHeader 
    }; 
    var that = this; 
    var req = http.request(options, function (res) { 

     res.setEncoding('utf8'); 
     console.log("statusCode: ", res.statusCode); 
     console.log("headers: ", res.headers); 

     res.on('data', function (chunk) { 
      if (Object.prototype.toString.call(callback) === '[object Function]') { 
       var result = JSON.parse(chunk); 

       callback.call(that, null, result); 
       return result; 
      } 
     }); 
    }); 

    var postData = querystring.stringify(data); 
    req.write(postData); 
    req.end(data); 

    req.on('error', function (err) { 
     if (Object.prototype.toString.call(callback) === '[object Function]') { 
      callback.call(that, err, null); 
     } 
    }); 

} 
ServerApi.prototype.createUserId = function (data, callback) { 
    var url = 'https://api.netease.im/nimserver/user/create.action'; 
    var postData = { 
     'accid': data['accid'] || '', 
     'name': data['name'] || '', 
     'props': data['props'] || '', 
     'icon': data['icon'] || '', 
     'token': data['token'] || '' 
    }; 
    this.postDataHttps(url, postData, callback); 
} 

答えて

0

あなたのサーバーコードにあります。 1つはhttp.createServer(function (req, res) {...}となります

reqresのパラメータはどうなっていますか?

イベントendの場合、それはreq.on('end' function...です。「データは別のサーバーから受信しました」というコメントが表示された直後に、次のようにすることができます。

HTTPレスポンスコード= 200でクライアントに応答を返すと、HTTPボディのメッセージは「完了」になります。レスポンスオブジェクトでできることはたくさんあることに注意してください。詳細については、ドキュメントを参照してください。

を参照してください:中国語
https://nodejs.org/api/http.html#http_response_writehead_statuscode_statusmessage_headers

OR (中文版)

http://nodeapi.ucdok.com/api/http.html#http_class_http_serverresponse_7847

クイック説明:
在服务器的代码req.on('end'...那里、你可以用resオブジェクト打回送你的AJAXクライアント。

+0

これが動作するかどうかお知らせください。お手伝いします。 –

+0

ありがとう、問題解決済み、ありがとう – neo

関連する問題