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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5545577/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 17:34:16  来源:igfitidea点击:

AJAX POST handler causing "uncaught exception"

phpjavascriptajaxpost

提问by SimonBarker

So I've been banging my head against my desk for a few hours on this one and i'm not getting anywhere so help would really be appreciated.

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

The code below has two jquery event handlers which fire off an ajax request. The first one uses GET and the data it gets back from the server is JSON encoded - it works fine. The second one ( "button#addTx" ) returns causes Firebug to produce this error:

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

uncaught exception: [Exception... "prompt aborted by user" nsresult: "0x80040111 (NS_ERROR_NOT_AVAILABLE)" location: "JS frame :: resource://gre/components/nsPrompter.js :: openTabPrompt :: line 468" data: no]

Line 0

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

第 0 行

which is no help to at all. The server side script is printing raw html to the screen and the aim is that a jquery html replace will be used to update to the page which initiates the request. The data is POSTed correctly as the database updates but beyond that I have no clue. I have rewritten it to try a GET and still produce the same error :-(

这根本没有帮助。服务器端脚本将原始 html 打印到屏幕上,目的是使用 jquery html 替换来更新启动请求的页面。数据在数据库更新时正确发布,但除此之外我不知道。我已经重写它以尝试 GET 并仍然产生相同的错误:-(

Help would be amazing - thank you, 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);
        }
    });
});
});

and here is the server side script

这是服务器端脚本

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

?>

回答by SimonBarker

PROBLEM SOLVED: so in the html markup the form that holds the fields of data should have an

问题已解决:因此在 html 标记中,包含数据字段的表单应该有一个

onsubmit="return false;"

in it!

在里面!

Thanks for all the help guys, I have implemented all your suggestions and my code is now soooo much smaller and easier to manage!

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

Cheers

干杯

Simon

西蒙

回答by RossGK

Thx for posting the solution. Similarly banged my head for a while trying to solve a similar problem with NS_ERROR_NOT_AVAILABLE without luck. Useful for for people using Django <--> Javascript to do XMLHttpRequests as well. On the Django side, there is an

感谢发布解决方案。类似地,我试图用 NS_ERROR_NOT_AVAILABLE 解决类似的问题,但没有运气,这让我头疼了一阵子。对于使用 Django <--> Javascript 来执行 XMLHttpRequests 的人也很有用。在 Django 方面,有一个

   error: [Errno 32] Broken pipe 

...that corresponds with the NS_ERROR that appears in the firebug console for the JS failure.(googleBait) It's hard to know where to start tracing the problem - server side or client side.

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

Thx again.

再次感谢。