標籤:des style http color io os 使用 java ar
跨域提交留言,並返回是否成功!
C#服務端:
public void FeedBackPost(string name, string email, string tel, string website, string desc, string thisIp, string sourse, string callBack) { try { Response.ContentType = "application/x-javascript"; string str = callBack; if (thisIp==null) { thisIp = "未取到IP"; } string strSql = ""; HSBY_DBEntities db = new HSBY_DBEntities(); //驗證ip短時間內是否提交太多 Regex rx = new Regex(@"((?:(?:25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d)))\.){3}(?:25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d))))"); if (rx.IsMatch(thisIp)) { strSql = @"select count(*) from fb_srv_main where ip='" + thisIp + "'"; int ipCount = db.ExecuteStoreQuery<int>(strSql).FirstOrDefault(); if (ipCount>10) { str += "([{\"請勿重複提交!\"}]);"; Response.Write(str); return; } } fb_srv_main newItem = new fb_srv_main(); newItem.name = name; newItem.email = email; newItem.tel = tel; newItem.site = website; newItem.content_str = desc; newItem.ip = thisIp; newItem.creat_by = sourse; newItem.creat_on = DateTime.Now; _mainBLL.Create(ref validationErrors, newItem); //HttpContext context = new HttpContext(); if (validationErrors.Count>0) { str+=validationErrors[0].ErrorMessage; Response.Write(str); return; } FeedBackEmail( name, email, tel, website, desc, thisIp, sourse); str += "([{\"Rusltstr\":\"OK\"}]);"; Response.Write(str); return; } catch (Exception ex) { Response.ContentType = "application/x-javascript"; Response.Write(callBack + "([{\"Rusltstr\":\"出錯了!\"}]);"); return; } }
JS用戶端:
//提交留言返回資料處理function displayResult(data) { var iRusltstr=data[0].Rusltstr; if(iRusltstr=="OK"){ //關閉遮罩$("#submitmask").fadeOut(500);alert("提交成功!"); }else{ $("#submitmask").fadeOut(500);alert(iRusltstr+" 請使用其他方式聯絡我們!"); } }
//提交留言function SubmitMsg(){var AllInput=$("#formwrap input");var iTextarea=$("#formwrap textarea");//獲得焦點樣式AllInput.focus(function(){AddFocusStyle($(this))});AllInput.blur(function(){AddFocusStyle($(this))});iTextarea.focus(function(){AddFocusStyle($(this))});iTextarea.blur(function(){AddFocusStyle($(this))});function AddFocusStyle(iInput){if(iInput.attr("style")=="" || iInput.attr("style")==null){iInput.attr("style","border:1px solid #bcbcbc;color:#7a7a7a;");}else{iInput.attr("style","");}}//丟失焦點驗證AllInput.blur(function(){FormValiudat($(this));});//表單驗證方法function FormValiudat($input){var promptStr="";var pattern = new RegExp("[~'!#$%^&*()-+_=]");if(pattern.test($input.val())){alert("非法字元!親,請勿嘗試注入哦!");return false;}switch($input.attr("id")){case "name": //姓if($input.val().length>10){$("#redpoint_name").removeClass("hide");$input.focus();return false;}else{$("#redpoint_name").addClass("hide");} break;case "email": //emailvar reg = /^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$/;if (!reg.test($input.val())) { $("#redpoint_email").removeClass("hide");$input.focus();return false;}else{$("#redpoint_email").addClass("hide");} break; case "tel"://電話var isMobile=/^(?:13\d|15\d|18\d)\d{5}(\d{3}|\*{3})$/; var isPhone=/^((0\d{2,3})-)?(\d{7,8})(-(\d{3,}))?$/;if(!isMobile.test($input.val()) && !isPhone.test($input.val())){$("#redpoint_tel").removeClass("hide");$input.focus();return false;}else{$("#redpoint_tel").addClass("hide");} break; case "website": if(!(/[^\d]/g).test($input.val())){$("#redpoint_website").removeClass("hide");$input.focus();return false;}else{$("#redpoint_website").addClass("hide");} break; case "desc": var mainText=$input.val(); if(mainText.length>=100){alert("最多100個字!");return false;} break;}return true;}//表單提交$("#formsubmit").click(function(){var $feedback_input=$("#formleft input");for(var i=0;i<$feedback_input.length;i++){var $thisInput=$($feedback_input[i]);if(!FormValiudat($thisInput)){return;};}//提交表單前開啟遮罩$("#submitmask").fadeIn(500); var url = "http://www.sparkdesign.cn:8888/MainAjax/FeedBackPost" url = "http://www.sparkdesign.cn:8888/MainAjax/FeedBackPost?name="+$("#name").val()+"&email="+$("#email").val()+"&tel="+$("#tel").val()+"&website="+$("#website").val()+"&desc="+$("#desc").val()+"&thisIp="+$("#keleyivisitorip").html()+"&callback=displayResult" $.getScript(url); })}
C#使用jsonp進行跨域提交表單