ReferenceError:未定义$ http

时间:2017-02-13 18:31:17

标签: javascript node.js

我正在尝试通过node.js进行SOAP调用,并收到以下错误:

ReferenceError: $http is not defined

这是我的代码,其他一切似乎都有效,直到它在最后一行失败:

//Import the `assert` module to validate results.
var assert = require('assert');
var SoapRequestXML='<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:_5="https://clients.mindbodyonline.com/api/0_5">\r\n' +
                   '<soapenv:Header/>\r\n' +
                   '<soapenv:Body>\r\n' +
                   '<_5:GetClasses>\r\n' +
                   '<_5:Request>\r\n' +
                   '<_5:SourceCredentials>\r\n' +
                   '<_5:SourceName>SOURCECREDENTIALS</_5:SourceName>\r\n' +
                   '<_5:Password>PASSWORD</_5:Password>\r\n' +
                   '<_5:SiteIDs>\r\n' +
                   '<_5:int>-99</_5:int>\r\n' +
                   '</_5:SiteIDs>\r\n' +
                   '</_5:SourceCredentials>\r\n' +
                   '</_5:Request>\r\n' +
                   '</_5:GetClasses>\r\n' +
                   '</soapenv:Body>\r\n' +
                   '</soap:Envelope>';


var options = {
    //Define endpoint URL.
    url: "https://api.mindbodyonline.com/0_5/ClassService.asmx",
    //Define body of POST request.
    body: SoapRequestXML,
    //Define insert key and expected data type.
    headers: {
         'POST': 'https://api.mindbodyonline.com/0_5/ClassService.asmx?wsdl HTTP/1.1',
         'Accept-Encoding': 'gzip,deflate',
         'Content-Type': 'text/xml;charset=UTF-8',
         'SOAPAction': '"http://clients.mindbodyonline.com/api/0_5/GetClasses"',
         'Content-Length': '594',
         'Host': 'api.mindbodyonline.com',
         'Connection': 'Keep-Alive',
         'User-Agent': 'Apache-HttpClient/4.1.1 (java 1.5)'


        }
};

//Define expected results using callback function.
function callback(error, response, body) {
    //Log status code to Synthetics console.
    console.log(response);
    //Verify endpoint returns 200 (OK) response code.
    assert.ok(response.statusCode == 200, 'Expected 200 OK response');
    //Parse JSON received from Insights into variable.
    //
   var parseString = require('xml2js').parseString;
   var XMLReSULT = response.body;
   parseString(XMLReSULT, function (err, result) {
    console.dir(result);

   });

    //Log end of script.
    console.log("End reached");
}

//Make GET request, passing in options and callback.
$http.post(options, callback);

感谢任何帮助,我对此非常陌生。

1 个答案:

答案 0 :(得分:0)

与隐含的先前评论一样,$http未定义,您无法使用var $http = require('http'),因为节点http没有post方法。你需要做一些重构。我想你正在寻找这样的东西。

var assert = require('assert')
var http = require('http')
var parseString = require('xml2js').parseString;

var SoapRequestXML='<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:_5="https://clients.mindbodyonline.com/api/0_5">\r\n' +
                   '<soapenv:Header/>\r\n' +
                   '<soapenv:Body>\r\n' +
                   '<_5:GetClasses>\r\n' +
                   '<_5:Request>\r\n' +
                   '<_5:SourceCredentials>\r\n' +
                   '<_5:SourceName>SOURCECREDENTIALS</_5:SourceName>\r\n' +
                   '<_5:Password>PASSWORD</_5:Password>\r\n' +
                   '<_5:SiteIDs>\r\n' +
                   '<_5:int>-99</_5:int>\r\n' +
                   '</_5:SiteIDs>\r\n' +
                   '</_5:SourceCredentials>\r\n' +
                   '</_5:Request>\r\n' +
                   '</_5:GetClasses>\r\n' +
                   '</soapenv:Body>\r\n' +
                   '</soap:Envelope>';

var options = {
  hostname: 'api.mindbodyonline.com',
  port: 80,
  path: '/0_5/ClassService.asmx',
  method: 'POST',
  headers: {
         'Accept-Encoding': 'gzip,deflate',
         'Content-Type': 'text/xml;charset=UTF-8',
         'SOAPAction': '"http://clients.mindbodyonline.com/api/0_5/GetClasses"',
         'Content-Length': '594',
         'Connection': 'Keep-Alive',
         'User-Agent': 'Apache-HttpClient/4.1.1 (java 1.5)'
  }
}

var req = http.request(options, (res) => {
  var data;

  res.setEncoding('utf8');
  res.on('data', (chunk) => {
    data += chunk;
  });
  res.on('end', () => {
    parseString(data, function(err, result) {
      console.log(result);
    });
    console.log('End reached')
  });
});

req.on('error', (e) => {
  console.log(`problem with request: ${e.message}`);
});

req.write(SoapRequestXML);
req.end();