2017-02-13 70 views
-1

我嘗試通過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); 

任何幫助表示讚賞,我對此很感興趣。

+4

在使用''http $ require = require('http')'之前是否導入了'http'模塊? –

+0

沒有顯示可以表明爲什麼應該定義 – charlietfl

+0

@ Sushanth--將該評論提升爲答案。 OP顯然包含了他的代碼的每一行,包括其他「require」,而其中不包含必要的代碼。 – Malvolio

回答

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