Javascript 是否可以在没有jquery的情况下使用AJAX删除请求
我想在不使用jquery的情况下使用AJAX delete request,那么我在localhost:8000上的json对象是否可以这样做:Javascript 是否可以在没有jquery的情况下使用AJAX删除请求,javascript,html,css,ajax,Javascript,Html,Css,Ajax,我想在不使用jquery的情况下使用AJAX delete request,那么我在localhost:8000上的json对象是否可以这样做: { "Students":[{"Name":"Kaushal","Roll_No":30,"Percentage":94.5}, {"Name":"Rohit","Roll_No"
{
"Students":[{"Name":"Kaushal","Roll_No":30,"Percentage":94.5},
{"Name":"Rohit","Roll_No":31,"Percentage":93.5},
{"Name":"Kumar","Roll_No":32,"Percentage":45.5}]}
我想有一个删除按钮,可以删除一个单一的记录。代码将不胜感激
函数loadDoc(){
var table2=“名称滚动\u百分比”
变量url2=”http://localhost:8000/Students"
var xhttp2=新的XMLHttpRequest();
xhttp2.onreadystatechange=函数(){
if(xhttp2.readyState==4&&xhttp2.status==200){
var jsonArr=JSON.parse(xhttp2.responseText);
对于(我在jsonArr中){
表2+=“”+jsonArr[i]。名称+
“+jsonArr[i]。卷号+
“”+jsonArr[i]。百分比+“”
}
document.getElementById(“mytable”).innerHTML=table2;
}
}
xhttp2.open('GET',url2,true);
xhttp2.send();
表,th,td{
边框:1px纯黑;
边界塌陷:塌陷;
}
获取数据
您可以使用JS Fetch API来完成此操作
const url = 'https://www.example.com/route';
const deleteFetch = async (url) => (
const response = await fetch(url, {
method: ‘DELETE’
});
const myJson = await response.json(); // converting to json
return myJson ;
}
这应该起作用:
function request() {
this.http = new XMLHttpRequest();
}
request.prototype.delete = function(url, callback) {
this.http.open("DELETE", url, true);
var self = this;
this.http.onload = function() {
if (self.http.status === 200) {
callback(null, "Record Deleted");
} else {
callback("Error: " + self.http.status);
}
};
this.http.send();
};
另一个例子(可能更清楚):
jQuery是javascript,因此它使用本机javascript方法进行ajax请求。因此答案是肯定的。我建议使用更新的方法来处理浏览器HTTP请求。它可以选择指定请求类型,如图所示,只需将“GET”更改为“DELETE”,并将负载放在
send()
中的两个参数之间。这种“是否可能”的问题通常指向缺乏研究,因为除了jQuery之外,还有太多的库使用ajax。这能回答您的问题吗?我想要一个可以删除单个记录的函数,只需在URL中包含要删除的记录。例如:。删除将由您使用的API完成。
deleteRecord('https://jsonplaceholder.typicode.com/posts/1');
function deleteRecord(url) {
var xhr = new XMLHttpRequest();
xhr.open("DELETE", url);
xhr.send();
xhr.onload = function() {
if (xhr.status != 200) {
console.log('ERROR');
} else {
console.log('DELETED!');
}
};
xhr.onerror = function() {
console.log('NO CONNECTION');
};
}