Post method, return text, background to get data
View
$.ajax ({type: "POST", DataType: "Text",//return type is a text URL: "/O Rder/ajaxgetcoupon?ids= "+id, data: {Conponcode:code, totalprice:totalprice}, suc Cess:function (data) {if (data = = "False") {showfailtip (' Server busy, please wait and try again! ‘); Return } else if (data = = "No") {Showfailtip (' the coupon you entered does not exist or has expired! ‘); Return } else if (data = = "used") {Showfailtip (' The coupon you entered has been used! ‘); Return } else if (data = = "Limit") {Showfailtip (' the coupon you entered does not meet the minimum spending amount! ‘); Return } else {$ ("#couponPriceId"). Text ("¥" + data.split ("|") [0]); $ ("#payPriceId"). Text ("¥" + data.split ("|") [1]); } } });
Code
[HttpPost]
Publicactionresult ajaxgetcoupon (formcollection fm,int IDs) {//ids for parameters in URL or backend for URL parameters request.querystring["IDs"] if(string. Isnullorwhitespace (fm["Conponcode"])) { return Base. Content ("False"); } stringCouponcode = fm["Conponcode"]; decimalnum = Globals.safedecimal (fm["Totalprice"], (decimal) 0M); Bll. Couponinfo Info=NewBLL. Couponinfo (); Model.couponinfo Couponinfo= info. Getcouponinfo (Couponcode,false); if(Couponinfo! =NULL) { if(Couponinfo.status = =2) { return Base. Content ("used"); } if(Couponinfo.limitprice >=num) { return Base. Content ("Limit"); } shoppingcartinfo Cartinfo=NewShoppingcarthelper (Base. Currentuser.userid). Getshoppingcart4selected (); Switch(info.) Getusestatus (Cartinfo, Couponinfo)) { Case 0: return Base. Content ("No"); Case 1: { stringSTR2 = (num-couponinfo.couponprice). ToString ("F"); return Base. Content (CouponInfo.CouponPrice.ToString ("F") +"|"+str2); } Case 2: return Base. Content ("used"); Case 3: return Base. Content ("Limit"); Case 4: return Base. Content ("Categorylimit"); Case 5: return Base. Content ("Productlimit"); Case 6: return Base. Content ("Skulimit"); Case 7: return Base. Content ("Categoryno"); Case 8: return Base. Content ("Productno"); Case 9: return Base. Content ("Skuno"); } } return Base. Content ("No"); }
If you need to return JSON, you will $.ajax in DataType: "JSON"
Backstage: Public Jsonresult ajaxgetcoupon (formcollection fm,int IDs) {
Return Json (New {Errcode =0,message= "very sorry"});
}
Page Pass Parameter
Location.href = "order/submitorder?sku=" + SKU + "&g=" + Groupbuyid;
Public ActionResult SubmitOrder (string sku, int count = 1, int c = 0, int g = 0, String viewName = "SubmitOrder")
{
Shoppingcartinfo cartinfo = new Shoppingcartinfo ();
Return base. View (ViewName, cartinfo);
}
MVC foreground Post/get parameter value problem when getting data asynchronously