我有一个mysql的输入文本字段.那么,如何通过ajax从数据库中输出json表呢

I have an input text field for mysql. So how can i output the json table from database through ajax?

本文关键字:ajax 何通过 数据库 表呢 json 输出 mysql 有一个 输入 文本 那么      更新时间:2023-09-26
$.ajax({
    type:'GET',
    url:'index.php',
    data: {val1},
    dataType: 'json',                        
    success: function(response) {
        for(i=0;i<response.length;i++) {
            var test = response[i];
            $("#demo").append(test);
            console.log(test);
            //$("demo").append(test[i]);
        }
        console.log(response);
    }
});

在您的站点中,如果您调用"index.php",您是否正确获得了json数组?如果是的话,在"index.php"页面中,您是否将标题设置为输出json类型的文本,如

header("Content-type: application/json; charset=utf-8'");

否则,结果页面将是一个普通的text/html页面,因此您的val(1)不会像json字符串一样被识别。

如果这不起作用,请尝试发布您的"index.php"页面