jquery ajax 調用失敗的原因樣本介紹_jquery

來源:互聯網
上載者:User
下面是一個完事的函數:
複製代碼 代碼如下:

function ReLoadPromotion() {
var A = parseFloat($("#pNormalTotal").html()) + parseFloat($("#pBarginTotal").html());
jQuery.ajax(
{
url: "/ajax/OrderRedemption.aspx?vf=getlist",
type: "POST",
dataType: "json",
data:{amount:A},
success: function (json) {
$("#fittingProductList tr.tableItem").remove();
var data = json.list;
$.each(data, function (i, n) {
var id = n.pid;
var row = "<tr id=\"trPromotin" + id + "\" class=\"tableItem\">";
row += "<td><input id=\"cbPromotionItem" + id + "\" type=\"checkbox\" value=\"" + n.pid + "\" name=\"promotionProduct\" title=\"" + n.price + "\" onclick=\"CalculateOrder()\" /></td>";
row += "<td><label for=\"cbPromotionItem" + id + "\">" + n.pname + "</label></td>";
row += "<td>滿" + n.amount + "</td>";
row += "<td>" + n.price + "</td>";
row += "</tr>";
$("#fittingProductList").append(row);
});
$("#promotionArea").slideDown();
CalculateOrder();
},
error: function (xml) {

//alert(arguments[1]);
var json = eval("(" + xml.responseText + ")");
$("#promotionArea").slideUp();
}
});
}

很多的時候你調試時會發現直接跳到error,然後直接就跳過去了.success段沒有被執行.其實這裡所有的語句都沒有問題,問題的關鍵在於傳回值.如果傳回值出錯,他就不會執行success段.傳回值出錯有幾種情況:

1.格式錯誤.這是比較簡單的,比較常見的是多了一個逗號之類:如{"success":true,"list":[{},{},]}(最後多一個逗號)正常不會有這個,容易出錯的時候是你用for語句時後面最後沒處理那個逗號.

2.類型錯誤:這種錯誤最難找,如果沒把握就用字元是最好的.比如:"success":true是可以的,"code":0,也可以.如果是"code":a123,就有問題,會出現轉換錯誤.要這樣才對:"code":"a123"

由於 jquery調試的時候會忽略錯誤,所以有時候會很抓狂.一個簡單的方法是寫error語句,加alert,錯誤會被捕捉到.
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.