Hi I am a beginner to Laravel and AJAX programming, and I am trying to send a JavaScript variable through the data field
Here is my code:
$.ajax({
type: "post",
url: "/checklists/" + checkButtonId,
data: {relevantClasses : JSON.stringify(relevantClasses)},
dataType: "json",
success: function (response) {
if(response.success) {
$(taskId).removeClass(relevantClasses);
alert(JSON.stringify(response.checked + " " + response.request + " " + response.id));
switch(response.checked) {
case 0:
$(taskId).addClass("far fa-square");
break;
case 1:
$(taskId).addClass("fas fa-check-square");
break;
}
}
}
});
The variable relevantClasses
is a JavaScript let
variable. I tried to stringify
it with JSON and it returns in the alert, but in the Controller I can't compare this variable with a PHP one. (I used json_decode()
function and nothing happens)
can you please help me?
you could send relevantClasses like this
$.ajax({
type: "post",
url: "/checklists/" + checkButtonId,
data: {
'relevantClasses' : JSON.stringify(relevantClasses)
},
dataType: "json",
success: function (response) {
if(response.success) {
$(taskId).removeClass(relevantClasses);
alert(JSON.stringify(response.checked + " " + response.request + " " + response.id));
switch(response.checked) {
case 0:
$(taskId).addClass("far fa-square");
break;
case 1:
$(taskId).addClass("fas fa-check-square");
break;
}
}
}
});
if it didn't work please upload your controller
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.