我写了一个html页面,它在php动作页面上做了一个简单的jquery ajax帖子。 1,第一部分,第二部分,第三部分,第四部分

I have an hmtl page that does a jquery ajax POST to a php action page, which queries MySQL and then echoes back data, which I capture (as the result variable) in the success function of my jquery ajax function.

Everything works fine, except that I can not get the php page to return the data in a format I can use.

I am trying to get it back in a multiple array or json format something like:

{"Sections":[{"sectionID":"1", "sectionText":"Section One"},{"sectionID":"2", "sectionText":"Section Two"},{"sectionID":"3", "sectionText":"Section Three"}]};

that I can parse and use.

However, this is the best I can get:

"1,Section One2,Section Two3,Section Three4,section four"

If i try to add any characters at all to the echo out, it goves by a bunch of jibberish that looks like html for a table with a bunch of errors thrown in.

Below is the PHP script

// Declare $sql variable with the SQL Query as a SQL Select Statement
    $sql="SELECT sectionID, sectionText FROM Sections";

// Store results of SQL query as a variable called "$result"
$result = mysqli_query($con,$sql);

if (!mysqli_query($con,$sql))
    {
        die('Error: ' . mysqli_error($con));
    }

// Do something with the results - making it a select
while($row = mysqli_fetch_array($result, MYSQLI_ASSOC))
    {
    echo $row ['sectionID'], ',', $row['sectionText'];
    }

Below is the javascript jquery ajax function from the html page

 $.ajax({
url:"querySections3.php",
success:function(result){
   alert(result);
}
 });
<?php

// Declare $sql variable with the SQL Query as a SQL Select Statement
$sql="SELECT sectionID, sectionText FROM Sections";

// Store results of SQL query as a variable called "$result"
$result = mysqli_query($con,$sql);

if (!mysqli_query($con,$sql))
{
   die('Error: ' . mysqli_error($con));
}

$sections = array();

// Do something with the results - making it a select
while($row = mysqli_fetch_array($result, MYSQLI_ASSOC))
{
    $sections[] = array('sectionId' => $row ['sectionID'], 'sectionText' => $row['sectionText']);
}

header('Content-type: application/javascript');
echo json_encode(array('sections' => $sections));

And in your ajax request add dataType: json