[英]How to write Ajax call delete method with specific id in vanilla javascript
How do I write this peace of Ajax call that is written in jQuery, completely in vanilla JavaScript 如何编写完全用普通JavaScript编写的用jQuery写的Ajax调用
What should be converted into vanilla javascript 什么应该转换为原始javascript
$.ajax({ type: "DELETE", url: "/users/delete/" + $(".deleteUser").data('id'); }}
What I think it should be, but the console log gave me the following error message : request is not defined. 我认为应该是什么,但是控制台日志给了我以下错误消息:请求未定义。 And I am afraid it would not pick up the id.
而且我恐怕不会领取身份证。
request.open("DELETE", "/users/delete/" + this.dataSet('id'));
You can't get any more vanilla then good old xmlhttprequest. 您再也无法获得比旧的xmlhttprequest更好的香草了。
var xhr1 = new XMLHttpRequest();
xhr1.open('DELETE', "http://127.0.0.1:80/users/delete/", true);
xhr1.onreadystatechange = function() {
if (this.status == 200 && this.readyState == 4) {
dostuff = this.responseText;
};//end onreadystate
xhr1.send();
Enjoy. 请享用。
request
is node module, it is not available in browser (altho there is a fork for browser, still it is not vanilla JS, but a library instead). request
是节点模块,在浏览器中不可用(虽然有一个fork的浏览器,但它不是vanilla JS,而是一个库)。
If you are fine with not supporting IE (just IE, edge should be fine), you can use fetch
API: 如果您不支持IE很好(只是IE,edge应该没问题),则可以使用
fetch
API:
fetch("/users/delete/" + $(".deleteUser").data('id'), {
method: "DELETE",
}}.then(function (response) {
console.log(response);
});
https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API
Btw I guess you also want to get rid of the $.data
call, this is vanilla JS version: 顺便说一句,我想你也想摆脱
$.data
调用,这是普通的JS版本:
var id = document.querySelector('.deleteUser').getAttribute('data-id');
You can simply use the new fetch API: 您可以简单地使用新的访存API:
fetch(url, {method: 'DELETE'}) fetch(url,{方法:“删除”})
.then((res)=>{
res.json()
window.location.href = '/'
}
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.