weixin_33713503 2014-02-15 10:29 采纳率: 0%
浏览 18

在jQuery中获取parsererror

I'm trying to do the following:

from a html page, pushing a button will call a php script which query a db and echoes json. Php page can be found at http://vscreazioni.altervista.org/prova.php and works fine. What doesn't work is jquery side, because I'm getting parsererror as response. Here's my code:

<!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>

I'm totally new at this stuff...any help would be appreciated

EDIT: php code from prova.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); 
?>

Edit 2: I was having a misspelling issue. Now I got SUCCESS!!! as reported in

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

how can alert json content? I tried with

alert(json);

but i get an alert with [object Object]

  • 写回答

1条回答 默认 最新

  • weixin_33728268 2014-02-15 11:05
    关注

    In success block do like following to get posts.

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

    this will alert "icostanza" value.

    评论

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?