在jquery中获取parsererror

时间:2014-02-15 10:29:16

标签: php jquery ajax json

我正在尝试执行以下操作:

从一个html页面,按下一个按钮将调用一个php脚本,该脚本查询数据库并回显json。 Php页面可以在http://vscreazioni.altervista.org/prova.php找到并且工作正常。 什么不起作用是jquery方面,因为我得到parsererror作为响应。 这是我的代码:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="initial-scale=1, maximum-scale=1" />
<style type="text/css">

</style>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function () { 
$('#button_1').click(function(e){
    e.preventDefault();
    e.stopPropagation();
    favfunct();
 });
});

function favfunct() {
$.ajax({
            type: 'GET',
            url: 'prova.php',
            dataType: 'json',
            success: function (json) {
            alert("SUCCESS!!!");

            },
            error: function (xhr, status) {
        alert(status);
    },
        });
}
</script>
</head>
<body>
<input id="button_1" type="button" value="push"  />
</body>
</html>

我对这些东西都很陌生......任何帮助都会受到赞赏

编辑: 来自prova.php的php代码

<?php

$conn = mysql_connect("localhost", “username”, “passwd”);

if (!$conn)
{
 mysql_close($conn); 
 die("Problemi nello stabilire la connessione");
}   

if (!mysql_select_db("my_vscreazioni"))
{
 mysql_close($conn); 
 die("Errore di accesso al data base utenti");
}

$queryIcostanza = "SELECT SUM(iCostanza) FROM apps";  
$resultIcostanza = mysql_query($queryIcostanza) or die(mysql_error());
$rowIcostanza = mysql_fetch_array($resultIcostanza);

$queryIversi = "SELECT SUM(iVersi) FROM apps";  
$resultIversi = mysql_query($queryIversi) or die(mysql_error());
$rowIversi = mysql_fetch_array($resultIversi);

$queryI10numeri = "SELECT SUM(i10Numeri) FROM apps";  
$resultI10numeri = mysql_query($queryI10numeri) or die(mysql_error());
$rowI10numeri = mysql_fetch_array($resultI10numeri);

$queryIcostanza4x = "SELECT SUM(iCostanza4x) FROM apps";  
$resultIcostanza4x = mysql_query($queryIcostanza4x) or die(mysql_error());
$rowIcostanza4x = mysql_fetch_array($resultIcostanza4x);

$queryOndanews = "SELECT SUM(OndaNews) FROM apps";  
$resultOndanews = mysql_query($queryOndanews) or die(mysql_error());
$rowOndanews = mysql_fetch_array($resultOndanews);

$queryFarmachimica = "SELECT SUM(FarmaChimica) FROM apps";  
$resultFarmachimica = mysql_query($queryFarmachimica) or die(mysql_error());
$rowFarmachimica = mysql_fetch_array($resultFarmachimica);

$queryIcarrano = "SELECT SUM(iCarrano) FROM apps";  
$resultIcarrano = mysql_query($queryIcarrano) or die(mysql_error());
$rowIcarrano = mysql_fetch_array($resultIcarrano);

$totale = 0;
$totaleIcostanza = $rowIcostanza['SUM(iCostanza)'];
$totaleIversi = $rowIversi['SUM(iVersi)'];
$totaleI10numeri = $rowI10numeri['SUM(i10Numeri)'];
$totaleIcostanza4x = $rowIcostanza4x['SUM(iCostanza4x)'];
$totaleOndanews = $rowOndanews['SUM(OndaNews)'];
$totaleFarmachimica =  $rowFarmachimica['SUM(FarmaChimica)'];
$totaleIcarrano = $rowIcarrano['SUM(iCarrano)'];

$totale = $totaleIcostanza + $totaleIversi + $totaleI10numeri + $totaleIcostanza4x +      $totaleOndanews + $totaleFarmachimica + $totaleIcarrano;

$comando = "select * from apps";

$result = mysql_query($comando) or die(mysql_error());

$ultima_data="";

while ( $dati = mysql_fetch_assoc($result) ) 
{
   $ultima_data = $dati['data']; 
}

$response = array();

$posts = array('icostanza'=> $totaleIcostanza, 'iversi'=> $totaleIversi, 'i10numeri'=> $totaleI10numeri, 'icostanza4x'=> $totaleIcostanza4x, 'ondanews'=>$totaleOndanews, 'farmachimica'=> $totaleFarmachimica, 'icarrano'=> $totaleIcarrano, 'totale'=>$totale, 'ultimo'=>$ultima_data);

$response['posts'] = $posts;

$json = json_encode($response);
echo $json;

mysql_close($conn); 
?>

编辑2: 我有一个拼写错误的问题。现在我成功了!如

中所述
success: function (json) {
            alert("SUCCESS!!!");         
}

如何提醒json内容?我试过

alert(json);

但是我收到[object Object]

的警告

1 个答案:

答案 0 :(得分:0)

在成功栏中,请按以下方式获取帖子。

success: function (json) {
            alert(json.posts.icostanza);

            },

这将提醒“icostanza”值。