如何在节点js中使用ajax?

时间:2014-11-23 13:53:00

标签: ajax node.js

我想在我的节点js项目中使用ajax,但在运行项目后,我看到以下错误:

TypeError: Object function ( w ) {
                if ( !w.document ) {
                    throw new Error( "jQuery requires a window with a document" );
                }
                return factory( w );
            } has no method 'ajax'

我的代码是:

                         var jq = require('jquery');
                        jq.ajax({
                              type: "POST",
                              url: "http://localhost/sms/index.php",
                            });

1 个答案:

答案 0 :(得分:2)

您可以使用request module(npm安装请求):

var request = require('request');
request({
  method: 'POST',
  url: 'http://localhost/sms/index.php',
  json: someJsonObject
  // or form depending on what http://localhost/sms/index.php accepts, 
  // see request document for more options
}, function (error, response, body) {
  if (!error && response.statusCode == 200) {
    console.log(body)
  }
});