【发布时间】:2018-04-12 20:09:02
【问题描述】:
我正在尝试向用户提供反馈。如果他们已经添加了一些东西,他们会收到一条消息,说这个景点已经添加到您的行程中。如果用户的行程中没有景点,他们将收到成功消息。虽然它总是说这个景点已经被添加了,即使它还没有。没有错误,有谁知道为什么会这样。下面是该函数的代码。
function addAttractionItinerary(ev) {
var id=$(ev.target).parents('div.Attraction')[0].$data.attraction_id;
$.post('php/validation.php', {"CHECKRECORD" :1 ,"id" : id}, function(data){
if (data = 1) {
alert("This attraction has already been added to your itinerary")
handleModalClose();
} else if (data = 0){
$.ajax({
url: 'php/itinerary.php',
type: 'POST', //will send a POST request to the PHP back-end with the "attractionId" the user has clicked on
dataType: 'json',
data: {
action: 'CREATE',
attractionId: $(ev.target).parents('div.Attraction')[0].$data.attraction_id //The $(ev.target).parents('div.attraction')[0].$data.attraction_id expression get's the "attractionId" of the clicked element
},
//It first get's the parent div with the class "attraction" reads the first one in the list [0] and then reads the $data property of this element
success: function(data) {
alert("The attraction is successfully added");
},
error: console.log
});
}
});
}
【问题讨论】:
-
“php/validation.php”中发生了什么?我猜这个函数总是返回 1。这是第一个看的地方
-
`"...没有错误..."*.你在自相矛盾。
-
确保使用
if(data == 0)而不是if(data = 0)。一个=将值分配给变量,而两个比较它们。
标签: javascript ajax