Hej,
Zgłaszam się do Was dzisiaj z uprzejmą prośbą o nakierowanie mnie, co robię źle przy próbie wysyłania zapytania do API. Tak, wiem, na necie jest pełno poradników dotyczących JS / AJAX / JQuery, jednakże zazwyczaj one dotyczą odczytywania danych z API, a moim celem jest stworzenie funkcji przesyłającej dane do API jednej z usług. Poniżej zamieszczam kod:
Oczywiście celowo podmieniłem API Key, także to nie ono jest problemem, sam klucz jest w pełni okej. ;)
<script>
function UserAction(){
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
// Typical action to be performed when the document is ready:
var response = xhr.responseText;
console.log("ok"+response);
}
};
xhr.open('POST','https://api5.esv2.com/v2/Api/Subscribers/', true);
xhr.send("POST https://api5.esv2.com/v2/Api/Subscribers/ HTTP/1.1 Content-Type: text/xml <ApiRequest xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\"> <ApiKey>KEY</ApiKey><Data xsi:type=\"Subscriber\"><Mode>AddAndUpdate</Mode><Force>true</Force><ListId>1</ListId><Email>[email protected]</Email><Firstname>John</Firstname><Lastname>Smith</Lastname><TrackingCode>123</TrackingCode><Vendor>xyz</Vendor><Ip>11.22.33.44</Ip><Properties><Property><Id>5</Id><Value xsi:type=\"xs:string\">student</Value></Property><Property><Id>3</Id><Value xsi:type=\"xs:dateTime\">1985-03-12</Value></Property></Properties></Data></ApiRequest>");
}