这次寒假有一个asp.net课设,题目是在线购物系统,所以想着做一个仿造小米商城,其中需要用到前端向后端传值。我用的ajax post,但是一直进入error 报Failed to load resource: the server responded with a status of 401 (Unauthorized)错
public static string Getit(string device)
{
return "qqq";
if (HttpContext.Current.Session["username"] != null)
{
return "您还未登陆" + device;
}
else
{
MyDb myDb = new MyDb();
string sql = "INSERT INTO [dbo].[Order] VALUES ("+(DateTime.Now-new DateTime(1970,1,1,0,0,0)).ToString()+" "+ HttpContext.Current.Session["username"]+" "+ HttpContext.Current.Session["name"]+" "+device+" "+DateTime.Now.ToString()+" 0 0)";
SqlParameter[] pas = { };
myDb.querry(sql);
return "购买成功";
}
}
<script>
$(function () {
$("td input").click(function () {
var device = $(this).parent("div").siblings("div").children("marquee").text();
//alert(name);
$.ajax({
type: "post",
url: '小米商城.aspx/Getit',
contentType: "application/json; charset=utf-8",
data: "{device:'" + device + "'}",
data:'json',
success: function (result) {
alert(result + "成功");
},
error: function (result) {
alert(result + "失败");
}
});
});
})
</script>
我的解答思路和尝试过的方法
我的想法是点一下加入购物车将得到的商品值传到后端进行处理,插入订单数据库。