duan19911992 2018-02-23 11:56
浏览 78

使用jQuery,AJAX获取编码的JSON数组

Good afternoon developers,

The last couple weeks I've been working on Ajax with jQuery, since it's a major element on modern websites nowadays. However, I simply can't retrieve an encoded JSON array, from my PHP file with jQuery. Because the most basic ability I at least should possess is reading out Mysql data with AJAX.

I've tried $.getJSON(url, data, response) , dataType: 'json' , but neighter solutions worked out, and I've also visited several websites including various StackOverflow questions...

This is the regarding PHP code that retrieves data through Mysql and converts it into a JSON object array:

  <?php //ajaxIndex.php

    include 'connectionFile1.php' ; //connection file 

                $sql1 = "SELECT * FROM users" ;

                if ($result1 = mysqli_query($connection, $sql1) ) {

                    $resultsSql1 = mysqli_num_rows($result1) ;

                    $return_array = array();

                    while ($row1 = mysqli_fetch_array($result1)) {
                        $row_array['userName'] = $row1['userName'];
                        $row_array['email'] = $row1['email'];

                        //$return_array[] = $row1 ;

                        array_push($return_array, $row_array);
                    }


                   //encode the mysql array

                    print json_encode($return_array); // this array is correctly encoded




                 }

        ?>

This is the jQuery code that should properly retrieve and assimilate the JSON object array:

<script>

//$(document).ready(fetchData) ;

$("#button").click(function () {


    $.ajax({ 

        url: "ajaxIndex.php",      
        type: "GET",
        dataType: 'json' ,                
        success: function(response){ 

            console.log(response) ;
            $.each(response, function() {

                        console.log(this.userName + "<br>") ;
                        console.log(this.email) ;

            }) ;

        }

    });


}) ;


//window.setTimeout(fetchData, 1000);

 </script>

Thank you dudes in advance.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥100 Jenkins自动化部署—悬赏100元
    • ¥15 关于#python#的问题:求帮写python代码
    • ¥20 MATLAB画图图形出现上下震荡的线条
    • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
    • ¥15 perl MISA分析p3_in脚本出错
    • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
    • ¥15 ubuntu虚拟机打包apk错误
    • ¥199 rust编程架构设计的方案 有偿
    • ¥15 回答4f系统的像差计算
    • ¥15 java如何提取出pdf里的文字?