簡體   English   中英

我的ajax調用返回帶有json數據的html

[英]my ajax call is returning html with json data

我有一個奇怪的問題,我試圖使用對我的php程序的ajax調用來填充數據表,該php程序從內部從數據庫獲取數據。

的PHP:

<?php
require_once('config.php');
$query = mysql_query("select * from productdetails");
while($fetch = mysql_fetch_array($query))
{
        $output[] = array ($fetch[0],$fetch[1],$fetch[2],$fetch[3],$fetch[4],$fetch[5]);
}
echo json_encode($output, JSON_FORCE_OBJECT);
?>

HTML(ajax呼叫):

$.ajax({
            url: 'process.php?method=fetchdata',
            data: "json",
            success: function(s){
            console.log($(s).text());
                    oTable.fnClearTable();
                        for(var i = 0; i < s.length; i++) {
                         oTable.fnAddData([
                                    s[i][0],
                                    s[i][1],
                                    s[i][2],
                                    s[i][3],
                                    s[i][4],
                                    s[i][5]
                                           ]);                                      
                                        } // End For

            },
            error: function(e){
               console.log(e.responseText); 
            }
            });

生成的輸出為


Call Stack #TimeMemoryFunctionLocation 10.0010242552{main}( )..\\process.php : 0 20.0010242840http://www.php.net/function.mysql-connect' target='_new'>mysql_connect ( )..\\process.php : 2 {"0":{"0":"1","1":"Iron","2":"AX12","3":"Google","4":"21.95","5":"HW"},"1":{"0":"2","1":"DartBoard","2":"AZ52","3":"Apple","4":"12.95","5":"SG"},"2":{"0":"3","1":"BasketBall","2":"BA74","3":"Microsoft","4":"29.95","5":"SG"},"3":{"0":"4","1":"Compopper","2":"BH22","3":"Google","4":"24.95","5":"HW"},"4":{"0":"5","1":"Gas Grill","2":"BT04","3":"Apple","4":"149.95","5":"AP"},"5":{"0":"6","1":"Washer","2":"BZ66","3":"Google","4":"399.99","5":"AP"}} (!)不建議使用:mysql_connect():不建議使用mysql擴展,並將在以后刪除:在第行的C:\\ wamp \\ www \\ datatableone \\ process.php中使用mysqli或PDO調用堆棧#TimeMemoryFunctionLocation 10.0010242552 {main }().. \\ process.php 0 20.0010242840http://www.php.net/function.mysql-connect'target ='_ new'> mysql_connect().. \\ process.php 2 {“ 0”: {“ 0”:“ 1”,“ 1”:“ Iron”,“ 2”:“ AX12”,“ 3”:“ Google”,“ 4”:“ 21.95”,“ 5”:“ HW”}, “ 1”:{“ 0”:“ 2”,“ 1”:“ DartBoard”,“ 2”:“ AZ52”,“ 3”:“ Apple”,“ 4”:“ 12.95”,“ 5”:“ SG“},” 2“:{” 0“:” 3“,” 1“:” BasketBall“,” 2“:” BA74“,” 3“:” Microsoft“,” 4“:” 29.95“,” 5“:” SG“},” 3“:{” 0“:” 4“,” 1“:” Compopper“,” 2“:” BH22“,” 3“:” Google“,” 4“:” 24.95“,” 5“:” HW“},” 4“:{” 0“:” 5“,” 1“:” Gas Grill“,” 2“:” BT04“,” 3“:” Apple“, “ 4”:“ 149.95”,“ 5”:“ AP”},“ 5”:{“ 0”:“ 6”,“ 1”:“ Washer”,“ 2”:“ BZ66”,“ 3”: “ Google”,“ 4”:“ 399.99”,“ 5”:“ AP”}}

但是我所需的輸出應該是:(僅json)

{“ 0”:{“ 0”:“ 1”,“ 1”:“ Iron”,“ 2”:“ AX12”,“ 3”:“ Google”,“ 4”:“ 21.95”,“ 5”: “ HW”},“ 1”:{“ 0”:“ 2”,“ 1”:“ DartBoard”,“ 2”:“ AZ52”,“ 3”:“ Apple”,“ 4”:“ 12.95”, “ 5”:“ SG”},“ 2”:{“ 0”:“ 3”,“ 1”:“籃球球”,“ 2”:“ BA74”,“ 3”:“ Microsoft”,“ 4”: “ 29.95”,“ 5”:“ SG”},“ 3”:{“ 0”:“ 4”,“ 1”:“ Compopper”,“ 2”:“ BH22”,“ 3”:“ Google”, “ 4”:“ 24.95”,“ 5”:“ HW”},“ 4”:{“ 0”:“ 5”,“ 1”:“ Gas Grill”,“ 2”:“ BT04”,“ 3” :“ Apple”,“ 4”:“ 149.95”,“ 5”:“ AP”},“ 5”:{“ 0”:“ 6”,“ 1”:“洗衣機”,“ 2”:“ BZ66” ,“ 3”:“ Google”,“ 4”:“ 399.99”,“ 5”:“ AP”}}

任何建議,請。

謝賽

解:

PHP:

<?php
$servername = "localhost";
$username = "root";
$password = "123456";

try {
        $conn = new PDO("mysql:host=$servername;dbname=holt", $username, $password);
        $statement=$conn->prepare("SELECT * FROM productdetails");
        $statement->execute();
        $results=$statement->fetchAll(PDO::FETCH_ASSOC);
        $json=json_encode($results);
        echo $json;
    } catch(PDOException $e) {
        echo "Connection failed: " . $e->getMessage();
    }
?>

感謝您的建議。

如注釋中所述,您應該關閉警告,或者更好的方式是,以不產生警告的方式編寫代碼。

關於關閉警告: 關閉php / mysql上的警告和錯誤

您可以使用@符號抑制內聯錯誤,該符號是php中的錯誤控制運算符。 將@放在mysql_connect()行的開頭應該可以擺脫它,但是您應該切換到PDO!

在PDO(我建議和使用)上: http : //code.tutsplus.com/tutorials/why-you-should-be-using-phps-pdo-for-database-access--net-12059

PDO保護您免受SQL注入的侵害,並允許將查詢發送到數據庫並事先進行構造,然后再通過“占位符”發送輸入。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM