admin管理员组文章数量:1199522
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.
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.
Share Improve this question edited Jun 11, 2017 at 16:55 HizkiFW 95 bronze badges asked Nov 7, 2013 at 15:55 tampietampie 1851 gold badge1 silver badge11 bronze badges 3 |3 Answers
Reset to default 16First of all, the code you posted is problematic 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"
.
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.
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);
本文标签: javascriptSending PUTDELETE data with a XMLHttpRequestStack Overflow
版权声明:本文标题:javascript - Sending PUTDELETE data with a XMLHttpRequest - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738542679a2095805.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
var objHTTP = new ActiveXObject("MSXML2.XMLHTTP");
- WHY are you using an oldIE-only method instead of the standardizedXMLHttpRequest
object every somewhat recent browser supports? And what about this horrible active waiting instead of using theonreadystatechange
callback? – ThiefMaster Commented Nov 7, 2013 at 16:00