使用nodeJS调用jQuery ajax

时间:2016-09-15 16:34:26

标签: javascript jquery ajax node.js api

这些是我的代码块。 API端点

      app.get('/clients/zvb/:id', function(req, res) {
console.log('ZVB Query loaded as var = file;')
var path = './public/queries/zvb.sql'
var zvb = fs.readFileSync(path, "utf8")
zvb = zvb.splice(25, 0, req.params.id)
request.query(zvb, function(err, recordset) {
  console.log(recordset);
  res.end(JSON.stringify(recordset));
});

})

然后我的index.html,我尝试从端点获取数据。

        $.ajax({
    type: 'GET',
    url: 'http://localhost:8081/clients/zvb/16601',
    dataType: 'jsonp',
    success: function(data) {
        console.log(data);
        console.log('we got to here..')
    }
});

此刻并没有发生太多事情。当我尝试在终端中手动运行ajax调用时,我得到了;

    Object {readyState: 1}

端点正在工作,如果我查看它,我可以看到我正在使用的JSON。

汤姆

1 个答案:

答案 0 :(得分:-1)

如果您使用的是jquery 1.5+,这些简写方法非常易于使用。 (我不能发表评论或者会问)。 Doc在这里http://api.jquery.com/category/ajax/shorthand-methods/

$.get( "http://localhost:8081/clients/zvb/16601", function(data) {
  var data = JSON.parse(data);
  })
  .done(function() {

  })
  .fail(function() {

  })
  .always(function() {

  });