0

嘿伙计们,我有以下 ajax 调用:

function sendUserfNotes()
{
    $.ajax({
    type: "POST",
    dataType: "json",
    url: '/pcg/popups/getNotes.php',
    data:
    {
        'nameNotes': notes_name.text(),
    },
    success: function(response) {
        $('#notes_body').text(response.the_notes);
        alert(response.the_notes);
        //$('#notes_body').html(data);
    }
});

一旦它运行并进入这个文件:.......

try {  
  # MySQL with PDO_MYSQL  
  $DBH = new PDO("mysql:host=$hostname;dbname=$database", $username, $password);  
  $DBH->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );

}  
catch(PDOException $e) { 
    echo "I'm sorry, I'm afraid I can't do that.";  
    file_put_contents('PDOErrors.txt', $e->getMessage(), FILE_APPEND);   
}

$username_notes = $_POST['nameNotes'];

$sql = "SELECT notes FROM csvdata WHERE username = :username";
$getmeminfo = $DBH->prepare($sql);
$getmeminfo->execute(array(':username' => $username_notes));
$row = $getmeminfo->fetch(PDO::FETCH_ASSOC);
$notes = $row['notes'];

$returnArray = array( 'the_notes' => $row['notes']);

echo json_encode($returnArray);
$DBH = null;

现在在这里,一旦返回 json 数组,$('#notes_body').text(response.the_notes);将使用返回的响应更改 div,但我的问题是我无法让它返回。它总是为空?

我不确定出了什么问题,所以如果你能这么好心,给我一些时间:)

大卫

更新:

所以一件事是我自己测试了 pdo 语句并且它有效。

如果我只是给 $username_notes 一个像“BillCosby”这样的直接名称,它会为那个人返回正确的值。

4

1 回答 1

3

There is no such thing like "issue with PDO in $.ajax". PDO is server side while $.ajax is client side.

It's like the pdo statement does not even run?

Nobody knows.
Nobody have access to your code but you.
So, you have to answer yourself. Debug your code.

For the client-side debugging Firebug is priceless.
Open it's Net tab and see if there was an HTTP call at all. If it was - did it return anything?
Open Console and see if there are any errors.

于 2013-02-10T17:43:38.033 回答