【发布时间】:2016-05-02 04:05:45
【问题描述】:
我在一个电子商务网站上工作,其中列出了多种产品,每种产品都有自己的输入字段和带有唯一 ID 的提交按钮。当我将不同的商品添加到购物车时,它可以正常工作,但是,当我在刷新页面之前多次添加相同的商品时,它不会发布。
这是我的 AJAX 代码:
function isNumber(n) {
return !isNaN(parseFloat(n)) && isFinite(n);
}
var debug = false;
$(document).ready(function () {
$('#productcontent').on('click', 'input[type="submit"]', function (event) {
event.preventDefault();
var inputID = this.name;
//alert($("#" + inputID).val());
var qty = $("#"+inputID).val();
if (!isNumber(qty)) {
$("#"+inputID).focus();
return;
}
var userQuantityText = $('#cart' + inputID).text();
var userQuantity = userQuantityText.slice(0,1);
var userQuantityInt = parseInt(userQuantity);
var combinedQty = userQuantityInt + parseInt(qty);
$.ajax({
type: "POST",
url: "pladditem.aspx",
data: { shoppingCartItemNum: inputID, shoppingCartItemQuan: qty },
success: function (htmldata) {
$("#"+inputID).val("");
$(".notifications").replaceWith("<span class='notifications'>" + htmldata + "</span>");
if ($('#cart' + inputID).length > 0) {
$('#cart' + inputID).replaceWith('<div id="cart' + inputID + '" class="items-in-cart">' + combinedQty + ' item(s) in cart</div>');
}
else {
$('<div id="cart' + inputID + '" class="items-in-cart">' + qty + ' item(s) in cart</div>').insertBefore("#" + inputID);
}
$('<div style="background-color:yellow;">' + "Added " + qty + " to your shopping cart!" + '</div>').insertBefore("#" + inputID).delay(3000).fadeOut();
//alert("Added " + qty + " to your shopping cart with " + htmldata + " items! Item #: " + inputID);
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
if (debug) {
alert(XMLHttpRequest.responseText);
alert(textStatus);
alert(errorThrown);
}
}
});
});
$('.quanityinput').keypress(function (event) {
if (event.which == 13) {
event.preventDefault();
var inputID = this.name.slice(1,9);
//alert(inputID);
var qty = $("#" + inputID).val();
//alert(qty);
if (!isNumber(qty)) {
$("#" + inputID).focus();
return;
}
var userQuantityText = $('#cart' + inputID).text();
var userQuantity = userQuantityText.slice(0, 1);
var userQuantityInt = parseInt(userQuantity);
var combinedQty = userQuantityInt + parseInt(qty);
$.ajax({
type: "POST",
url: "pladditem.aspx",
data: { shoppingCartItemNum: inputID, shoppingCartItemQuan: qty },
success: function (htmldata) {
$("#" + inputID).val("");
$(".notifications").replaceWith("<span class='notifications'>" + htmldata + "</span>");
if ($('#cart' + inputID).length > 0) {
$('#cart' + inputID).replaceWith('<div id="cart' + inputID + '" class="items-in-cart">' + combinedQty + ' item(s) in cart</div>');
}
else {
$('<div id="cart' + inputID + '" class="items-in-cart">' + qty + ' item(s) in cart</div>').insertBefore("#" + inputID);
}
$('<div style="background-color:yellow;">' + "Added " + qty + " to your shopping cart!" + '</div>').insertBefore("#" + inputID).delay(3000).fadeOut();
//alert("Added " + qty + " to your shopping cart with " + htmldata + " items! Item #: " + inputID);
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
if (debug) {
alert(XMLHttpRequest.responseText);
alert(textStatus);
alert(errorThrown);
}
}
});
}
});
});
这是在后端运行的 VB.NET 代码:
Partial Class pladditem
Inherits System.Web.UI.Page
Dim connection As New Data.SqlClient.SqlConnection
Dim query As New Data.SqlClient.SqlCommand
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Session("Username") <> "" Then
Dim shoppingcartitems As String = ""
Dim itemno As String = ""
itemno = Request.Form("shoppingCartItemNum")
Dim itemqty As String = ""
itemqty = Request.Form("shoppingCartItemQuan")
connection.ConnectionString = "Data Source=blah blah blah;Initial Catalog=blah;User ID=blah;password=blah"
query.Connection = connection
query.Parameters.Clear()
'''' CHANGES QUANTITY FOR A SINGLE ITEM ON THE ORDER
query.Connection = connection
query.Parameters.Clear()
query.Parameters.Add("@USERNAME", Data.SqlDbType.VarChar).Value = Session("Username")
''' WILL PROBABLY NEED TO BE CHANGED
query.Parameters.Add("@QUANTITY", Data.SqlDbType.VarChar).Value = Request.Form("shoppingCartItemQuan")
query.Parameters.Add("@ITEM", Data.SqlDbType.VarChar).Value = Request.Form("shoppingCartItemNum")
''''''''''''''''''''''''''''''''''''
query.CommandText = "exec Web_AddToCart @USER = @USERNAME, @QTY = @QUANTITY, @ITEMNO = @ITEM"
query.Connection.Open()
query.ExecuteNonQuery()
query.Connection.Close()
query.CommandText = "select count('1') from weboeordd where orduniq not in (select orduniq from weboeordsubmit) and orduniq in (select orduniq from weboeordh where [user] = @username)"
Dim dt As New Data.DataTable
Dim da As New Data.SqlClient.SqlDataAdapter(query)
da.Fill(dt)
If dt.Rows.Count > 0 Then
Response.Write(dt.Rows(0).Item(0).ToString)
Else
Response.Write("")
End If
End If
End Sub
End Class
【问题讨论】:
-
第一次发帖后有没有查看错误日志,看看有没有js错误?
-
@b3tac0d3 是的,我的 AJAX 请求没有错误。
标签: jquery sql asp.net ajax vb.net