2

php スクリプトに json_encode がありますが、json_encode が while ループ内で呼び出されると、Jquery 側で失敗するようです。

ページ以外のブラウザー ウィンドウで結果を表示すると、実際に機能することがわかります。

例えば:

while ($row = mysql_fetch_array($result)) {     
    $id = $row['id'];
    $title = $row['title'];
    $content = $row['content'];

    $data = array("id" => $id,
    "title" => $title,
    "success" => true
    );
echo json_encode($data); // Inside the While Loop

}

ブラウザの出力は次のとおりです。

{"id":"15","title":"Advanced Booking Examples","success":true}
{"id":"14","title":"Advanced Booking Fields","success":true}
{"id":"11","title":"Add New Driver","success":true}
{"id":"10","title":"Registering a Driver \/ Add New Vehicle","success":true}
{"id":"8","title":"Dispatch Controls","success":true}
{"id":"7","title":"Troubleshooting Driver Device Checklist","success":true}
{"id":"6","title":"Requirements Checklist","success":true}

jQuery応答では、これは実際には空白ですがecho json_encode($data);、whileループのすぐ外側を離れると、jQueryは応答を取得しますが、ループ内の最後のレコードである1レコードのみです。

ループからすべての結果を取得するには、jQuery が必要です。

JSはこちら

$.ajax({ // Send it off for prcessing
        type: "POST",
        dataType: 'json',
        url: "includes/auto_suggest.php",
        data: {
            q: inputString
        },
        success: function (result) {
            if (result.success) {             
                var id = result.id;
                var title = result.title;                    
                $('#auto_id').append("<input type='text' class='hideid' id='hideid_" + id + "' value='" + id + "'/>");              
        $('#auto_title').prepend("<p>" + title + "</p>");
            }
        }
    });

何か案は?

前もって感謝します

4

8 に答える 8

8

JSONはすべて1つの配列またはオブジェクト上にある必要があります。各行を配列に追加してから、それを追加する必要がありますjson_encode

$data = array();
while ($row = mysql_fetch_array($result)) {     
    $id = $row['id'];
    $title = $row['title'];
    $content = $row['content'];

    $data[] = array(
      "id" => $id,
      "title" => $title,
      "success" => true
    );
}
echo json_encode($data);

次に、JavaScriptで、ループできるオブジェクトの配列が作成されます。

$.ajax({ // Send it off for prcessing
    type: "POST",
    dataType: 'json',
    url: "includes/auto_suggest.php",
    data: {
        q: inputString
    },
    success: function (result) {
        $.each(result, function(){
          if (this.success) {
            var id = this.id;
            var title = this.title;
            $('#auto_id').append("<input type='text' class='hideid' id='hideid_" + id + "' value='" + id + "'/>");
            $('#auto_title').prepend("<p>" + title + "</p>");
         }
        });
    }
});
于 2012-06-07T18:32:30.580 に答える
3

さて、あなたは有効なJSONを送信していません。オブジェクトをjson_encode配列に格納し、ループの後にその配列を格納します。

$data = array();
while ($row = mysql_fetch_array($result)) {     
    $id = $row['id'];
    $title = $row['title'];
    $content = $row['content'];

    $data[] = array("id" => $id,
        "title" => $title,
        "success" => true
    );
}
echo json_encode($data);

次に、オブジェクトではなく配列を適切に処理するようにJavaScriptコードを変更する必要があります。ここでは誰もあなたが何をしようとしているのか正確にはわからないので、あなたは自分でそれをするか、より多くの情報を提供しなければなりません。

于 2012-06-07T18:32:18.183 に答える
2

結果のデータを配列に追加し、次のjson_encodeように1回呼び出す必要があります。

$data = array();
while ($row = mysql_fetch_array($result)) {     
    $id = $row['id'];
    $title = $row['title'];
    $content = $row['content'];

    $data[] = array("id" => $id,
    "title" => $title,
    "success" => true
    );
}

echo json_encode($data);

これにより、出力されたJSONが変更され(現在JSONは無効であるため)、jQueryコールバックでループする必要があります。

于 2012-06-07T18:32:54.863 に答える
2

その行がループ内にあるときに返されるJSONは、全体として有効ではありません。複数のJSON文字列が一緒になっています。それらを分割する必要があります。

配列を作成し、ループ内でそれぞれ$dataをその配列に追加します。次に、ループの外側で、json_encodeそれをエコーし​​ます。これにより、JavaScriptに返すJSON文字列が1つ作成されます。

于 2012-06-07T18:33:10.100 に答える
1

多分...

$data = array();
while( WHATEVER ){
    // other code
    $data[] = array('id' => $id, 'title' => $title, 'success' => true);
}

echo json_encode($data);

すべての情報を1つの配列に格納してから、その配列をエンコードします。

于 2012-06-07T18:32:34.187 に答える
1

そのような複数のオブジェクトをjQueryに渡すことはできません。whileループが完了したら、in配列を配置し、オブジェクトの配列を出力します。

于 2012-06-07T18:32:44.720 に答える
1

whileループの外側でjsonをエンコードする必要があります。それは実際には正しい単一のjsonを返します

while ($row = mysql_fetch_array($result)) {     
    $id = $row['id'];
    $title = $row['title'];
    $content = $row['content'];

    $data[] = array("id" => $id,
    "title" => $title,
    "success" => true
    );


}
echo json_encode($data); 
于 2012-06-07T18:32:52.350 に答える
1

JSON 出力が無効です。この完全なコードを試してください(編集):

$result = Array();
while ($row = mysql_fetch_array($result)) {     
    $id = $row['id'];
    $title = $row['title'];
    $content = $row['content'];

    $data = array("id" => $id,
        "title" => $title,
    );
    $result[] = $data;
}
echo json_encode(Array('data'=>$result,'success'=>true)); // Outside the While Loop

そしてあなたのJavaScriptコードで

$.ajax({ // Send it off for prcessing
        type: "POST",
        dataType: 'json',
        url: "includes/auto_suggest.php",
        data: {
            q: inputString
        },
        success: function (results) {
            if (results.success) {
                for(var i in results.data) {
                    var result = results.data[i];             
                    var id = result.id;
                    var title = result.title;                    
                    var $newfield = $("<input type='text' class='hideid' id='hideid_" + id + "' value='" + id + "'/>");              
                    $newfield.prepend("<p>" + title + "</p>");
                    $('#auto_title').append($newfield);
                }
            }
        }
    });
于 2012-06-07T18:35:56.830 に答える