Ajax多行

时间:2015-11-05 21:42:51

标签: php jquery ajax

我有两个问题:

1。 我从query.php得到了我的Ajax:

echo json_encode($records, JSON_UNESCAPED_UNICODE);

给我这样的东西:

[{"cinfo_id":"25","fullName":"علی علوی","phone":"123456","mail":"a@a.com","description":"توضیحات علی علوی","jinfo_id":"2","jobTitle":"شرکت","jobName":"گلخانه","city":"مشهد"},{"cinfo_id":"26","fullName":"مصطفی کلانتر","phone":"09153040406","mail":"M.Mastersoft@gmail.com","description":"توضیحات مصطفی","jinfo_id":"3","jobTitle":"شرکت","jobName":"مهندسی","city":"تهران"}]
倾销的是:

Array
(
    [0] => Array
        (
            [cinfo_id] => 25
            [fullName] => علی علوی
            [phone] => 123456
            [mail] => a@a.com
            [description] => توضیحات علی علوی
            [jinfo_id] => 2
            [jobTitle] => شرکت
            [jobName] => گلخانه
            [city] => مشهد
        )

    [1] => Array
        (
            [cinfo_id] => 26
            [fullName] => مصطفی کلانتر
            [phone] => 09153040406
            [mail] => M.Mastersoft@gmail.com
            [description] => توضیحات مصطفی
            [jinfo_id] => 3
            [jobTitle] => شرکت
            [jobName] => مهندسی
            [city] => تهران
        )

)

一切看起来都很好,Ajaxy!但在我的php文件中我无法读取它:

var key = {
    'command': 'search',
    'data': $("#inputsearch").val()
};
$.ajax({
    type: 'POST',
    url: 'query2.php',
    data: key,
    dataType: 'json',
    success: function (msg) {

        $('#jobtitle').html(msg.jobTitle);
        $('#jobname').html(msg.jobName);
        $('#fullname').html(msg.fullName);
        $('#phone').html(msg.phone);
        $('#mail').html(msg.mail);
        $('#city').html(msg.city);
        $('#description').html(msg.description);

    }
})

但当我更换" []"在返回的json中使用"",

{"cinfo_id":"26","fullName":"مصطفی کلانتر","phone":"09153040406","mail":"M.Mastersoft@gmail.com","description":"توضیحات مصطفی","jinfo_id":"3","jobTitle":"شرکت","jobName":"مهندسی","city":"تهران"}

这是正确的。有什么问题?

  1. 我怎样才能显示多行?在返回的json数组中,我有所有数据。像:
  2. [{"cinfo_id":"25","fullName":"علی علوی","phone":"123456","mail":"a@a.com","description":"توضیحات علی علوی","jinfo_id":"2","jobTitle":"شرکت","jobName":"گلخانه","city":"مشهد"},{"cinfo_id":"26","fullName":"مصطفی کلانتر","phone":"09153040406","mail":"M.Mastersoft@gmail.com","description":"توضیحات مصطفی","jinfo_id":"3","jobTitle":"شرکت","jobName":"مهندسی","city":"تهران"}]
    Array
    (
        [0] => Array
            (
                [cinfo_id] => 25
                [fullName] => علی علوی
                [phone] => 123456
                [mail] => a@a.com
                [description] => توضیحات علی علوی
                [jinfo_id] => 2
                [jobTitle] => شرکت
                [jobName] => گلخانه
                [city] => مشهد
            )
    
        [1] => Array
            (
                [cinfo_id] => 26
                [fullName] => مصطفی کلانتر
                [phone] => 09153040406
                [mail] => M.Mastersoft@gmail.com
                [description] => توضیحات مصطفی
                [jinfo_id] => 3
                [jobTitle] => شرکت
                [jobName] => مهندسی
                [city] => تهران
            )
    
    )
    

    但我不知道如何展示它们。 提前谢谢。

1 个答案:

答案 0 :(得分:2)

如果您的msg变量是对象数组,则应将其作为对象数组访问

$('#jobtitle').html(msg[0].jobTitle);
$('#jobname').html(msg[0].jobName);
$('#fullname').html(msg[0].fullName);
$('#phone').html(msg[0].phone);
$('#mail').html(msg[0].mail);
$('#city').html(msg[0].city);
$('#description').html(msg[0].description);