Sending PUT/DELETE data with a XMLHttpRequest

谁说胖子不能爱 提交于 2019-12-06 18:06:28

问题


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.


回答1:


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".




回答2:


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.



来源:https://stackoverflow.com/questions/19840509/sending-put-delete-data-with-a-xmlhttprequest

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!