1

我很难让 jquery.post() 正常工作。一切似乎都井井有条,但我无法让我的 json 显示在屏幕上。我已经为此工作了大约最后一天左右,我在这里发帖作为最后的手段。我在这里看到了很多关于 ajax.post 的问题和很多好的答案。然而,这些问答中的大多数都涉及回显简单的变量。即单个int。不幸的是,由于我正在使用数组,我认为相同或相似的解决方案对我不起作用。这是返回到第一页的 php。我们将此部分称为第 2 页

if (!($stmt = $mysqli->prepare("SELECT id, todos FROM to_do_list WHERE uid =?"))) {
    echo "Prepare failed: (" . $mysqli->errno . ") " . $mysqli->error;
    }

    if (!$stmt->bind_param("s", $_SESSION['SESS_ID'])){
        echo "Binding param failed: (" . $stmt->errno . ") " . $stmt->error;
    }

    if (!$stmt->execute()){
        echo "Execute failed block 2: (" . $stmt->errno . ") " .$stmt->error;
    }
    $stmt->bind_result($id, $todos);
    if ($stmt->store_result()){
        if($stmt->num_rows > 0){
        while($stmt->fetch()){
                  $results[] =  array('id' => $id, 'todos' => $todos);     
        }
        echo json_encode($results);
    }

以下是第一页或调用页面的相关部分:

        <div id="content">


    </div>


    <form title="todosubmitter" id="todoPost" autocomplete="off">
        Add To Do:
        <br>
        <input type="text" name="todo" class="required formStyle error">
        <!--<input type="reset" name="submit" value="Submit" onclick="$.post('dbAccessor.php', $('#todoPost').serialize());"> -->
         <input type="reset" name="submit" value="Submit" onclick="$.post('dbAccessor.php', $('#todoPost').serialize(), function(data) { $('#content').html(data);}, 'json');"> 
    </form>

无论我在那里放什么,我都无法让我的任何 json 数据显示在屏幕上。我已经尝试了很多东西。没有成功。

还。在您的 php 不起作用并且没有将任何内容返回到第一页之前:

[{"id":98,"todos":"asdfa"},{"id":97,"todos":"asdfasdf"},{"id":96,"todos":"adsfasdfadf"},{"id":95,"todos":"asdfasdf"},{"id":94,"todos":"sadfasdf"},{"id":93,"todos":"asdfasdf"},{"id":92,"todos":"adfgadfga"},{"id":91,"todos":"asdfasd"},{"id":90,"todos":"asdfadf"},{"id":89,"todos":"asdfafd"},{"id":88,"todos":"asdf"},{"id":87,"todos":"asdf"},{"id":86,"todos":"asdf"},{"id":85,"todos":"asdf"},{"id":84,"todos":"dfa"},{"id":83,"todos":"asdf"},{"id":82,"todos":"do"},{"id":81,"todos":"asdf"},{"id":80,"todos":"asdf"},{"id":79,"todos":"asdf"}]

提前感谢谁能为我结束这个谜团。

4

0 回答 0