我想做的是在我的计数输入框中显示我的ajax结果。
我尝试使用 alert()
来了解我的 AJAX 是否返回一个值并且它给出了值。
我的问题是它没有在我的计数输入框中显示 AJAX 中的结果。
脚本:
$(document).ready(function(){
var countTimer = setInterval(
function ()
{
codeValue();
}, 500);
function codeValue(){
var data = {};
data.countValue = $('#countValue').val();
$.ajax({
type: "POST",
url: "codeTime.php",
data: data,
cache: false,
success: function (result) {
alert(result);
$("#count").val(result.user_code);
}
});
};
});
回应:
{"user_code":2}
最佳答案
您的 php 代码返回 json 字符串,而不是 javascript 对象。您可以指定dataType
或在回调函数中自行解析它。
$.ajax({
type: "POST",
url: "codeTime.php",
data: data,
cache: false,
dataType: "json",
success: function (result) {
alert(result);
$("#count").val(result.user_code);
}
});
或
$.ajax({
type: "POST",
url: "codeTime.php",
data: data,
cache: false,
success: function (result) {
result = JSON.parse(result); // parse the json string into a javascript object
$("#count").val(result.user_code);
}
});
关于javascript - 在文本框中显示 ajax 结果时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25219950/