【问题标题】:Getting parsererror in jquery在 jquery 中获取解析器错误
【发布时间】:2014-02-15 10:29:16
【问题描述】:

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

在 html 页面中,按下按钮将调用 php 脚本,该脚本查询 db 并回显 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] 的警报

【问题讨论】:

  • 请发布您的 PHP 代码。
  • 已发,请看
  • 尝试使用console.log(json) 在javascript控制台中查看对象

标签: php jquery ajax json


【解决方案1】:

在成功区块中,按以下方式获取帖子。

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

            },

这将提醒“icostanza”值。

【讨论】:

  • 这样你就可以获得所有的帖子数据。例如,alert(json.posts.iversi);
  • 您必须按照我所说的 json.posts.iversi、json.posts.icostanza4x、json.posts.ondanews 等方式收集所有值。
猜你喜欢
  • 2016-07-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-06
  • 2013-05-19
  • 2014-09-21
相关资源
最近更新 更多