perhaps? 2016-09-15 16:34 采纳率: 100%
浏览 455

使用nodeJS的jQuery ajax调用

These are my code blocks. API Endpoint

      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));
});

})

Then my index.html where I try and GET the data from the endpoint.

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

Not much is happening at the moment. When I try and manually run the ajax call in the terminal I get;

    Object {readyState: 1}

The endpoint is working, if I view it I can see the JSON i'm after.

Tom

  • 写回答

1条回答 默认 最新

  • weixin_33697898 2016-09-15 17:09
    关注

    If you are using jquery 1.5+ these shorthand methods are very easy to use. (I can't comment or would have asked). Doc is here 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() {
    
      });
    
    评论

报告相同问题?