0

I used the example from Send POST data using XMLHttpRequest to create this JavaScript code:

function PostXML(webURL, post_data) {
    var objHTTP = new ActiveXObject("MSXML2.XMLHTTP");
    objHTTP.open("POST", webURL, false);
    objHTTP.setRequestHeader("Content-Type", "application/xml; charset=utf-8");
    objHTTP.setRequestHeader("Accept", "application/xml; charset=utf-8");
    objHTTP.setRequestHeader("Content-Length", post_data.length);
    objHTTP.send(post_data);

    while((objHTTP.readyState != 4) && (objHTTP.readyState != 'complete')) {
        Delay(100);
    }

    if(200 != objHTTP.Status) {
        Log.Message("Returned Status Code of: " + objHTTP.Status);
        Log.Message("Status Text: " + objHTTP.StatusText);
    }
    else {
        Log.Message("Returned Status Code of: " + objHTTP.Status);
    }

    return objHTTP.responseText;
}

I also need to PUT and DELETE stuff. How do I transfer this code to be able PUT, and how do I transfer this code to be able to DELETE?

Any other examples which work the same is fine too.

HizkiFW
  • 9
  • 5
tampie
  • 145
  • 1
  • 1
  • 9
  • 14
    `var objHTTP = new ActiveXObject("MSXML2.XMLHTTP");` - WHY are you using an oldIE-only method instead of the standardized `XMLHttpRequest` object every somewhat recent browser supports? And what about this horrible active waiting instead of using the `onreadystatechange` callback? – ThiefMaster Nov 07 '13 at 16:00
  • 4
    Has -5 upvotes and over 15k views. – Sorter Jun 14 '16 at 04:40
  • I think here at least the tagging should make clear that it is an IE-only question. – peterh Dec 13 '17 at 11:11

3 Answers3

18

First of all, the code you posted is horrible and you should not be using it. See my comment on your question for some of reasons why.

To use PUT or DELETE instead of POST simply change the first argument you pass to objHTTP.open() to "PUT" or "DELETE".

ThiefMaster
  • 298,938
  • 77
  • 579
  • 623
  • 2
    Sorry for posting horrible code. I just started writing javascript for test automation purposes 2 months ago. No coding/scripting experience before this. If you could give me a complete example of how it should be I can learn from this. The replacing does work, thanks! The thing I did wrongly was that I replaced also the "post_data" by "put_data". Thanks for your help! – tampie Nov 07 '13 at 16:25
  • 2
    The first answer in the question you already linked to is a good start... http://stackoverflow.com/a/9713078/298479 Also, please don't use internet explorer during development except for testing if stuff works there, too. – ThiefMaster Nov 07 '13 at 16:26
  • It's not that horrible IMO. At least it's properly indented. I've seen worse. but ThiefMaster has a point. Why not do this the "proper way"? I don't see any reason. – Rolf Feb 15 '16 at 10:02
  • The OP has already told us the reason for the horrible code. He's a beginner. Let's not overly critical. – Umar Farooq Khawaja Jan 27 '17 at 23:22
  • @UmarFarooqKhawaja It's 2018 and it was 2013 so he isn't a beginner anymore. – Boy pro Aug 29 '18 at 19:56
  • The question was asked (and answered) in 2013. – Umar Farooq Khawaja Aug 29 '18 at 21:41
4

You want to send PUT or DELETE instead of POST? Have you tried replacing "POST" in the code with "PUT" or "DELETE"? (it's on the 3rd line of the code you posted).

BTW - this is a really bad example of how to implement httprequests from Javascript.

symcbean
  • 46,644
  • 6
  • 56
  • 89
  • 3
    Thanks! It works! See my previous comment. I'm just a very very beginner in JavaScript and used an example from the internet and adapted it. Any full examples how it should be implemented are very welcome! – tampie Nov 07 '13 at 16:27
1

You can try below code:

Update a user

var url = "http://localhost:8080/api/v1/users";

var data = {};
data.firstname = "John2";
data.lastname  = "Snow2";
var json = JSON.stringify(data);

var xhr = new XMLHttpRequest();
xhr.open("PUT", url+'/12', true);
xhr.setRequestHeader('Content-type','application/json; charset=utf-8');
xhr.onload = function () {
    var users = JSON.parse(xhr.responseText);
    if (xhr.readyState == 4 && xhr.status == "200") {
        console.table(users);
    } else {
        console.error(users);
    }
}
xhr.send(json);

Delete a user

var url = "http://localhost:8080/api/v1/users";
var xhr = new XMLHttpRequest();

xhr.open("DELETE", url+'/12', true);
xhr.onload = function () {
    var users = JSON.parse(xhr.responseText);
    if (xhr.readyState == 4 && xhr.status == "200") {
        console.table(users);
    } else {
        console.error(users);
    }
}
xhr.send(null);
Prasenjit Mahato
  • 816
  • 11
  • 8