2013-12-19 84 views
0

我试图通过使用GET方法调用Ajax发送状态信息给客户端。 我的数据到Nodejs服务器通过罚款,但我不能发回数据到我的client.I使用expressjs作为我的框架。 我知道数据存在,因为当我CONSOLE.LOG(误差)我得到如 一个对象{[错误:[email protected]已经subsribed]},但我不能输出这个消息给客户端。从发送的NodeJS数据,客户端不能正常工作

这里是route.js:它捕获(\订阅)上app.js

function signUp(name, email){ 
    var MailChimpAPI = require('mailchimp').MailChimpAPI; 
    var API_KEY = 'asdfasdf', 
      LIST_ID = 'asdfasdfas'; 
    var api = new MailChimpAPI(API_KEY , { version : '1.3', secure : false }); 
    var names = name.split(' '), status; 
    var data ={ 
      apikey: API_KEY, 
      id: LIST_ID, 
      email_address : email, 
      merge_vars: {'FNAME': names[0], 'LNAME': names[1]}, 
      double_optin: false, 
      send_welcome: true 
     }; 

    api.listSubscribe(data, function(error) { 

      if(error != null){ 
       console.log(error); 
       status = error; 
      }else { 
      console.log('success'); 
       status= '{success}'; 
      } 
     }); 

    return status; 
} 


exports.send = function(req, res, next){ 
    var name = req.query.name, email = req.query.email; 
    res.writeHead(200, {"Content-Type": "json"}); 
    res.send(signUp(name, email)); 
    res.end(); 

} 

这里是我的客户端:(返回null)

$.ajax({ 
    url:'subscribe', 
    method: 'GET', 
    data: {name: name, email: email}, 
    dataType: 'JSON', 
    success: function(res){ 
    alert(res); 
    } 
+0

那么应该如何布局是什么样子?任何例子? – ravi

回答

0

我不知道您使用的API(mailchimp),所以解决方案可能完全错误。但通常你可以看到同步和异步编程的区别。请阅读一些教程,以充分理解Node.js的方式,因为它需要一些时间来接受这种编程:-)

最重要的是,函数通常不会返回值,而是调用回调函数,功能(有时未命名为callback,但是done),当它们准备就绪或遇到错误时。

function signUp(name, email, callback) { // <----- provide a callback 
    var MailChimpAPI = require('mailchimp').MailChimpAPI; 
    var API_KEY = 'asdfasdf', 
     LIST_ID = 'asdfasdfas'; 
    var api = new MailChimpAPI(API_KEY, { version: '1.3', secure: false }); 
    var names = name.split(' '), status; 
    var data = { 
     apikey: API_KEY, 
     id: LIST_ID, 
     email_address: email, 
     merge_vars: {'FNAME': names[0], 'LNAME': names[1]}, 
     double_optin: false, 
     send_welcome: true 
    }; 

    api.listSubscribe(data, function (error, result) { 
     if (error) { 
      console.log(error); 
      callback(error); // <--- this is one of the "exit"-points of the function 
     } else { 
      console.log('success'); 
      callback(null, result); // <--- this is another 
     } 
    }); 
    // <----------- no return! nobody cares about the return value. 
} 


exports.send = function (req, res, next) { 
    var name = req.query.name, email = req.query.email; 
    res.writeHead(200, {"Content-Type": "json"}); 
    signUp(name, email, function (err, result) { 

     // this function gets called when signUp is done (maybe seconds later) 

     if (err) { 
      res.send(500); 
      return; 
     } 
     // don't know what the result looks like 
     res.send({status: 'success'}); 
     // res.end(); DON'T USE res.end() in express! 
    }); 
}; 
+0

嘿快问题,是否可以添加返回语句到nodejs中的函数? – ravi

+0

没关系。调用程序将始终忽略异步函数的返回值,并且可以像使用所有其他常用编程语言一样对待同步函数。即使回调函数没有返回“undefined”以外的值,您通常也可以看到'return callback(error)'。这只是为了获得较短的代码,但可能会误导没有经验的读者。 – hgoebl

相关问题