Javascript 如何在ajax中定义一个事后处理变量?
我使用ajax进程修改index.php文件上的用户状态 它可以工作,但我想给用户状态的div函数上色 我的代码: 我认为问题来自var status2= 如何修复此问题?HTML 剧本 错误在于调用函数后导入了jquery文件 因此,在调用函数之前进行导入 您的错误是在调用函数后进行了导入,这就是为什么会出现未定义的错误。正如您所说,您在页面中回显字符串,然后您可以根据以下代码直接从数据中检查此字符串 脚本: HTML: 函数recupstatut{ $.post'recup.php',functiondata{ 控制台日志数据; $'.cont2'.htmldata; var status2=数据; 如果状态2==对齐{ $'cont2'.cssbackgroundColor,4CAF50; }否则{ $'cont2'.cssbackgroundColor,f44336; } }; }Javascript 如何在ajax中定义一个事后处理变量?,javascript,jquery,ajax,Javascript,Jquery,Ajax,我使用ajax进程修改index.php文件上的用户状态 它可以工作,但我想给用户状态的div函数上色 我的代码: 我认为问题来自var status2= 如何修复此问题?HTML 剧本 错误在于调用函数后导入了jquery文件 因此,在调用函数之前进行导入 您的错误是在调用函数后进行了导入,这就是为什么会出现未定义的错误。正如您所说,您在页面中回显字符串,然后您可以根据以下代码直接从数据中检查此字符串 脚本: HTML: 函数recupstatut{ $.post'recup.php',fu
setIntervalrecupstatut,1000 有很多方法可以实现这一点。通过将$.post作为变量发送,可以使用$.post函数。例如:
// Fire off the request to /form.php
request = $.post({
url: "recup.php",
});
// Callback handler that will be called on success
request.done(function (response, textStatus, jqXHR){
// Log a message to the console
console.log("Hooray, it worked!");
});
// Callback handler that will be called on failure
request.fail(function (jqXHR, textStatus, errorThrown){
// Log the error to the console
console.error(
"The following error occurred: "+
textStatus, errorThrown
);
});
// Callback handler that will be called regardless
// if the request failed or succeeded
request.always(function () {
// Reenable the inputs
$inputs.prop("disabled", false);
});
或者我建议按如下方式使用$.ajax{}函数:
// Fire off the request to /form.php
$.ajax({
url: "recup.php",
type: "post",
data: { //specify data to be sent },
beforeSend:function(){
/* before sending the data to the other page
may be a loader to show waiting animation
*/
},
success:function(status){
/* this will check the response received from the previous page
and the determine the below conditions
*/
if (status == "En-ligne") {
content.style.backgroundColor = "#4CAF50";
} else {
content.style.backgroundColor = "#f44336";
}
}
});
做一个console.logstatus2,看看结果如何。在我看来,它永远不会对齐您被指定返回到.cont并将其从cont2中拉出。我现在将执行console.loggiorgio@MCMXCIIHTML:所以他可以使用或。放置一些console.log,在ajax中尝试获取JSON而不是HTML。尝试status2.trim以去除任何空白-尽管我同意@Kaddath,直接从响应中使用值,而不是附加值,然后再次选择。他正在设置并从同一个DIVi获取数据。我从他的问题中发布了该值。这在逻辑上与OP已经具有的相同-这不起作用。这就是为什么我要求他执行console.logdata;要查看ajax返回的内容,它应该是注释,而不是答案——事实上已经是了。数据总是没有问题,代码总是有其他问题!请解释一下??
<div class="cont2" id="cont2"></div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<script>
function recupstatut() {
$.post('recup.php', function(data) {
console.log(data);
var status2 = data.trim();
console.log(status2);
$('.cont2').html(status2);
if (status2 == "En-ligne") {
content.style.backgroundColor = "#4CAF50";
} else {
content.style.backgroundColor = "#f44336";
}
});
}
setInterval(recupstatut, 1000);
</script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<script>
$(function(){
function recupstatut() {
$.post('recup.php', function(data) {
$('#cont2').html(data); // If the data return from the php page as a string then you can compare it directly.
if (data == "En-ligne") {
$('#cont2').css("backgroundColor","#4CAF50");
} else {
$('#cont2').css("backgroundColor","#f44336");
}
});
}
setInterval(recupstatut, 1000);
});
</script>
<div class="cont2" id="cont2"></div>
// Fire off the request to /form.php
request = $.post({
url: "recup.php",
});
// Callback handler that will be called on success
request.done(function (response, textStatus, jqXHR){
// Log a message to the console
console.log("Hooray, it worked!");
});
// Callback handler that will be called on failure
request.fail(function (jqXHR, textStatus, errorThrown){
// Log the error to the console
console.error(
"The following error occurred: "+
textStatus, errorThrown
);
});
// Callback handler that will be called regardless
// if the request failed or succeeded
request.always(function () {
// Reenable the inputs
$inputs.prop("disabled", false);
});
// Fire off the request to /form.php
$.ajax({
url: "recup.php",
type: "post",
data: { //specify data to be sent },
beforeSend:function(){
/* before sending the data to the other page
may be a loader to show waiting animation
*/
},
success:function(status){
/* this will check the response received from the previous page
and the determine the below conditions
*/
if (status == "En-ligne") {
content.style.backgroundColor = "#4CAF50";
} else {
content.style.backgroundColor = "#f44336";
}
}
});