2017-02-13 3 views
-1
definiert ist

Ich versuche, einen SOAP-Aufruf durch node.js zu machen und die unten Störung erhalte:

ReferenceError: $http is not defined 

Hier ist mein Code, scheint alles andere zu arbeiten, bis es ausfällt die letzte Zeile:

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

Jede Hilfe wird geschätzt, ich bin sehr neu in diesem Bereich.

+4

Haben Sie das Modul "http" importiert, bevor Sie es verwenden? Let $ http = require ('http'); '? –

+0

Nichts gezeigt würde zeigen, warum es definiert werden sollte – charlietfl

+0

@ Sushanth-- diesen Kommentar zu einer Antwort zu fördern. Der OP hat anscheinend jede einzelne Zeile seines Codes eingeschlossen, einschließlich anderer "erfordert", und der notwendige ist nicht unter ihnen. – Malvolio

Antwort

0

Wie vorherige Kommentare impliziert, $http ist nicht definiert, und Sie können var $http = require('http') nicht verwenden, da Knoten http keine Post-Methode hat. Sie müssen ein wenig Refactoring machen. Ich glaube, du suchst so etwas.

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