2014-01-07 71 views
1

我真的很喜歡這裏的幫助,通常我通過閱讀現有的帖子發現任何問題。然而這一次我無法安靜地弄清楚我的代碼中有什麼問題。Ajax和php響應不起作用

我想在窗體中寫入特定Tablerow的dataildata值。該數據存儲在一個MySQL數據庫,我用PHP訪問它。

問題是,似乎Ajax請求不起作用。成功或者錯誤事件被觸發。雖然alter(id)工作得很好。

下面是函數的調用Ajax:

$("#table1 tbody tr").on("click", function() { 
    var id = $(this).attr('id'); 
    alert(id); 
    $.ajax({ 
    type:  'POST', 
    url:  'getDetailData.php', 
    dataType: 'json', 
    data:  {id : id }, 
    success: function(data){ 
     $("#inputWstId").val(data.WST-ID); 
     $("#inputSerialNumber").val(data.SERNO);  
     $("#inputName").val(data.NAME); 
     alert(data.NAME); 
     alert ("Test"); 
    }, 
    error:  function (xhr, ajaxOptions, thrownError) { 
     alert(xhr.status); 
     alert(thrownError); 
    } 
    }); 
}); 

這裏是PHP文件:

<?php 
    header('Content-Type: application/json'); 
    $connect = include 'connect.php'; 
    if (isset ($_POST['id'])){ 
     $id= $_POST['id']; 
    } 
    $query = " select * FROM pci WHERE id= ". $id ; 
    $result = mysql_query($query);  
    $ligne = mysql_fetch_array($result); 
    $data = array(
     "WST-ID" => $ligne["WST_ID"], 
     "SERNO"  => $ligne["SERNO"], 
     "NAME" => $ligne["NAME"] 
    ); 
    mysql_close($connect); 
    echo (json_encode($data)); 
?> 

如果你需要更多的源代碼或其他任何東西只是讓我的專有技術謝謝這麼多你的幫助!

WST_ID而不是WST-ID真的工作 - 非常感謝你! 爲什麼它,我不能在我的價值中使用「 - 」...猜測還有很多要學習;)

+0

這是什麼顯示瀏覽器控制檯?..你可以檢查網絡選項卡,實際看到的請求被拋出.. –

+0

一段時間POST不使用Ajax正常工作嘗試使用$ _REQUEST而不是和echo(json_encode($ data))後添加一個退出;行,你不需要頭('Content-Type:application/json');也可以使用。使用console.log(data)檢查你是否在.php文件中獲得json響應 –

+1

評論代碼,並打印一些文本以查看php代碼是否存在問題。另外檢查螢火蟲的CONSOL和淨選項卡 –

回答

-1

我認爲你需要給絕對的網址。只有getDetailData.php無效。嘗試http://xyzabc.com/getDetailData.php

+0

雖然這個鏈接可能會回答這個問題,最好在這裏包含答案的基本部分,並提供參考鏈接。如果鏈接頁面發生變化,則僅鏈接答案可能會失效 – Robert

+0

這是示例鏈接而不是實際鏈接答案提供,而不是鏈接 –

0
In the PHP part you need add two more headers : 

    header("Access-Control-Allow-Origin: *"); 
    header('Access-Control-Allow-Methods: GET, POST'); 

Add the two line of coding with the: 

     header('Content-Type: application/json'); 

change the: 

     echo (json_encode($data)); 

to: 

     print $my_json = json_encode($data); 

or: 

     print_r ($my_json); 



Hope it will work.