首页 > 解决方案 > 没有警报消息,Ajax 成功不起作用

问题描述

没有警报消息,Ajax 成功不起作用。控制台中没有错误。

$.ajax({
            url: "<?php echo base_url(); ?>Sensor/ConnectionTypeList",
            type: "POST",
            data: {'model_id': model_id},
            dataType: 'json',
            success: function (data) {

                console.log(data); //not runnig
                //alert(''running);

                if (document.getElementById("offset")) {
                    document.getElementById("offset").value = data[0].offset;
                }

                if (document.getElementById("multiplier")) {
                    document.getElementById("multiplier").value = data[0].multiplier;
                }

                if (document.getElementById("func")) {
                    document.getElementById("func").value = data[0].func;
                }

                if (document.getElementById("meas_command")) {
                    document.getElementById("meas_command").value = data[0].meas_command;
                }

                if (document.getElementById("read_command")) {
                    document.getElementById("read_command").value = data[0].read_command;
                }
            },
            error: function () {
                alert('Error.');
            }
        });

标签: phpjsonajax

解决方案


结果类型是 JSON 吗?在这种情况下,您需要解析返回的结果才能使用它,如下所示:

$.ajax({
        url: "<?php echo base_url(); ?>Sensor/ConnectionTypeList",
        type: "POST",
        data: {'model_id': model_id},
        dataType: 'json',
        success: function (data) {
            data = JSON.parse(data);
            console.log('>>', data);
            ...

我总是在 a 中使用“>>”(或类似的东西)console.log来确保您始终看到控制台消息,即使结果为空。检查控制台日志以查看结果及其类型。


推荐阅读