AJAX POST处理程序导致“未捕获的异常”

时间:2011-04-04 23:41:30

标签: php javascript ajax post

所以我一直在我的桌子上敲我的头几个小时,而且我没有得到任何地方,所以真的很感激帮助。

下面的代码有两个jquery事件处理程序,可以触发ajax请求。第一个使用GET,它从服务器返回的数据是JSON编码的 - 它工作正常。第二个(“按钮#addTx”)返回导致Firebug产生此错误:

  未被捕的例外:[例外......   “提示被用户中止”nsresult:   “0x80040111(NS_ERROR_NOT_AVAILABLE)”   位置:“JS框架::   资源://gre/components/nsPrompter.js   :: openTabPrompt :: line 468“数据:   没有]

     

第0行

根本没有帮助。服务器端脚本将原始html打印到屏幕,目的是使用jquery html替换来更新发起请求的页面。数据库更新时数据正确POST,但除此之外我没有任何线索。我重写了它以尝试GET并仍然产生相同的错误: - (

帮助会很棒 - 谢谢你,Simon

$(document).ready(function(){
$("button.delete").click(function(){
    var txid = this.id;
    var amountID = "#amount" + txid;
    var amount = $(amountID).html();
    // <![CDATA[

    var url = "delete.php?txid=" + txid + "&am=" + amount;
    $.ajax({
        type: "GET",
        url: url,
        success: function(msg){
            txid = "ul#" + txid;
            $(txid).hide();

            var values = msg;
            var e = "#" + values.category + "AmountLeft";
            var a = values.amount;

            $(e).html(a);
        }
    });
});
$("button#addTx").click(function(){

    // <![CDATA[


    var url = "addTran.php";
    //var dataV = var data = "category=" + document.getElementById("category").value + "&what=" + document.getElementById("what").value + "&amount=" + document.getElementById("amount").value + "&date=" + document.getElementById("date").value;
    $.ajax({
        type: "POST",
        url: "addTran.php",
        //async: false,
        data: "category=Groceries&what=Food&amount=2.33&date=2/3/2011",
        success: function(msg){
            $("transList").replaceWith(msg);
        }
    });
});
});

这是服务器端脚本

<?php
session_start();
include('functions.php');
//if the user has not logged in
if(!isLoggedIn())
{
    header('Location: index.php');
    die();
}


$category = $_POST['category'];
$what = $_POST['what'];
$amount = $_POST['amount'];
$date = $_POST['date'];

$category = mysql_real_escape_string($category);
$what = mysql_real_escape_string($what);
$amount = mysql_real_escape_string($amount);
$date = mysql_real_escape_string($date);

$date = convertDate($date);

//add trans to db
include('dbcon.php');
$query = "INSERT INTO transactions ( category, what, amount, date) VALUES ( '$category','$what','$amount','$date');";
mysql_query($query);

//grab the remaining amount from that budget
$query = "SELECT amount_left FROM cards WHERE category = '$category';";
$result = mysql_query($query);
$row = mysql_fetch_array($result, MYSQL_ASSOC);
$oldAmountLeft =  $row["amount_left"];

//update the amount left
$amountLeft = $oldAmountLeft - $amount;

mysql_free_result($result);

//add new value to db
$query = "UPDATE cards SET amount_left = '$amountLeft' WHERE category = '$category';";
mysql_query($query);



//generate the list of remaining transactions, print to screen to send back to main page

$query = "SELECT txid, what, amount, date FROM transactions WHERE category = ('$category');";
$result = mysql_query($query);

while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
    $d = convertDateReverse($row["date"]);
    $what = $row["what"];
    $amount = $row["amount"];
    $txid = $row["txid"];
    ?>
        <li><ul class="trans" id="<? echo $txid; ?>"><li class="date"><? echo $d; ?></li><li class="what"><? echo $what; ?></li><li class="amount" id="amount<? echo $txid; ?>"><? echo $amount; ?></li><button class="delete" id="<? echo $txid; ?>">Delete</button><li></li></ul></li>
    <?
}
mysql_free_result($result);



mysql_close();

header("Content-type: application/x-www-form-urlencoded"); //do I need this? I have a " header("Content-type: application/json"); " in the working one

?>

2 个答案:

答案 0 :(得分:19)

问题已解决:所以在html标记中,保存数据字段的表单应该有

onsubmit="return false;"

在里面!

感谢所有帮助人员,我已经实施了您的所有建议,现在我的代码更小,更易于管理!

干杯

西蒙

答案 1 :(得分:0)

发布解决方案的Thx。同样地试图用NS_ERROR_NOT_AVAILABLE解决类似的问题,但没有运气。适用于使用Django&lt; - &gt;的人Javascript也可以做XMLHttpRequests。在Django方面,有一个

   error: [Errno 32] Broken pipe 

...对应于出现在JS故障的firebug控制台中的NS_ERROR。(googleBait)很难知道从哪里开始跟踪问题 - 服务器端或客户端。

再次感谢。