[英]AJAX: Can't pass javascript variable to SQL query in PHP script
[英]Can't pass variable into ajax url in javascript
我基本上是想删除mongodb中的项目。 但是我似乎无法将ID传递给ajax调用中的url。 这是我的代码:
$(".delete-item").on('click', function(e, id) {
var deleteName = $('p.nameLink').text();
// Get ID first
$.ajax({
type: "GET",
url: "/items",
dataType: 'json',
})
.done(function(result) {
// searches mongodb for the clicked name and saves the result in the var
var newResult = $.grep(result, function(e){ return e.name === deleteName; });
var id = newResult[0]._id;
})
.fail(function (jqXHR, error, errorThrown) {
console.log(jqXHR);
console.log(error);
console.log(errorThrown);
});
// Then delete
console.log("ID to DELETE:", id); // I can see id
function itemDelete(id) {
$.ajax({
type: 'DELETE',
url: '/items/' + id,
dataType: 'json',
})
.done(function(result) {
console.log("ID to DELETE:", id); // Can't see the id
})
.fail(function (jqXHR, error, errorThrown) {
console.log(jqXHR);
console.log(error);
console.log(errorThrown);
});
}
itemDelete(id); // pass the id to the function
});
我现在只是在学习,所以我可能会以错误的方式进行操作。 如果有人可以帮助我,我将不胜感激。
错误消息是:
删除https://www.someplace.com/items/undefined 500(内部服务器错误)
由于ajax调用是异步的,因此您应该从第一个.done回调内部调用itemDelete(id) 。 尝试移动
itemDelete(id);
刚过
var id = newResult[0]._id;
这样,您将仅在第一个ajax调用终止后才执行第二个ajax调用,并且将定义id变量。
更改后,您的代码应如下所示:
$(".delete-item").on('click', function(e, id) {
var deleteName = $('p.nameLink').text();
$.ajax({
type: "GET",
url: "/items",
dataType: 'json',
})
.done(function(result) {
var newResult = $.grep(result, function(e){
return e.name === deleteName;
});
var id = newResult[0]._id;
itemDelete(id);
})
.fail(function (jqXHR, error, errorThrown) {
console.log(jqXHR);
console.log(error);
console.log(errorThrown);
});
function itemDelete(id) {
$.ajax({
type: 'DELETE',
url: '/items/' + id,
dataType: 'json',
})
.done(function(result) {
})
.fail(function (jqXHR, error, errorThrown) {
console.log(jqXHR);
console.log(error);
console.log(errorThrown);
});
}
});
这应该为您工作。
$(“。delete-item”)。on('click',function(e,id){var deleteName = $('p.nameLink')。text();
//首先获取ID
$.ajax({
type: "GET",
url: "/items",
dataType: 'json',
})
.done(function(result) {
// searches mongodb for the clicked name and saves the result in the var
var newResult = $.grep(result, function(e){ return e.name === deleteName; });
var id = newResult[0]._id;
itemDelete(id); // pass the id to the function
})
.fail(function (jqXHR, error, errorThrown) {
console.log(jqXHR);
console.log(error);
console.log(errorThrown);
});
console.log("ID to DELETE:", id); // I can see id
function itemDelete(id) {
$.ajax({
type: 'DELETE',
url: '/items/' + id,
dataType: 'json',
})
.done(function(result) {
console.log("ID to DELETE:", id); // Can't see the id
})
.fail(function (jqXHR, error, errorThrown) {
console.log(jqXHR);
console.log(error);
console.log(errorThrown);
});
}
});
谢谢
https://www.someplace.com/items/undefined
的undefined
表示ID未定义。 请注意,get和delete请求是异步发生的,因此不能保证在发出Delete请求时设置id
,因为它取决于请求完成的时间。 换句话说,请在获取ID 后尝试删除。 这将要求您在GET请求的成功回调中发出DELETE请求。
请试试这个
$.ajax({
type: 'get',
dataType : 'html',
url: 'your_url',
data:{variablname:id,type:'DELETE'}
dataType: 'json',
})
在网址页面或操作中获取值,因为其类型为get,因此数据类型为GET
if(isset($_GET['variablename'],$_GET['type'])){
if($_GET['type']=='DELETE')
delete record from database where id= $_GET['variablename']
}
希望对您有帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.