javascript - 在文本框中显示 ajax 结果时出现问题

标签 javascript php jquery html ajax

我想做的是在我的计数输入框中显示我的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/

相关文章:

javascript - 页面加载时,jQuery触发点击事件

jquery - 检查 Capybara::Node::Element 是否已被 jQuery.remove() 删除

javascript - 使用 Google 登录 使用移动设备/其他笔记本电脑从本地主机登录进行测试

javascript - 如何使用angular cli升级次要版本的angular

PHP 数组到 javascript 的转换改变了索引值

php - 获取在 magento 中应用的税收规则的名称

javascript - 使用 javascript 将 HTML 页面与 SQL 服务器连接

javascript - indexedDB onupgradeneeded 事件永远不会完成

javascript - 如何从 HTML 源刷新/重新加载我的 DataTable

php - 如何将 php ajax 文本框值插入到 mysql 数据库表中?