xml 使用 XMLHttpRequest 发送 PUT/DELETE 数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19840509/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Sending PUT/DELETE data with a XMLHttpRequest
提问by tampie
I used the example from Send POST data using XMLHttpRequestto create this JavaScript code:
我使用使用 XMLHttpRequest 发送 POST 数据中的示例来创建此 JavaScript 代码:
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 PUTand DELETEstuff. How do I transfer this code to be able PUT, and how do I transfer this code to be able to DELETE?
我也需要PUT和DELETE东西。我如何转移这个代码才能PUT,以及我如何转移这个代码才能DELETE?
Any other examples which work the same is fine too.
任何其他工作相同的例子也很好。
回答by ThiefMaster
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".
要使用 PUT 或 DELETE 而不是 POST 只需更改传递objHTTP.open()给"PUT"or的第一个参数"DELETE"。
回答by symcbean
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).
您想发送 PUT 或 DELETE 而不是 POST?您是否尝试用“PUT”或“DELETE”替换代码中的“POST”?(它在您发布的代码的第 3 行)。
BTW - this is a really bad example of how to implement httprequests from Javascript.
顺便说一句 - 这是如何从 Javascript 实现 httprequests 的一个非常糟糕的例子。

