This time I will bring you Ajax form submission and background processing. What are the precautions for Ajax form submission and background processing? The following is a practical case, let's take a look.
First of all, let’s talk about form submission. To submit the form, you must first collect the form data (as for verification, I won’t talk about it. I’ll leave it for next time). With jquery, you can get the html. The value is still as simple as $("xxid").val() and so on, but if a form collects a lot of data, and there are many forms like this, it will definitely be troublesome to use this method, and it is easy to make mistakes in recording. Therefore, we can simply define a collection rule. For example, we can mark the data form control that is to be sent back to the server, and then retrieve the marked data together. Let’s take the simplest stylistic input as an exampleUs Add a "datafield" attribute, and the stored value is the attribute name of the corresponding server-related class. With this mark, it will be easier to retrieve data from the front desk.We can define a general method such as the following code
getFormData: function(formid) { var data = {}; //获取TEXT文件内容 $("#" + formid + " input[type=text]").each(function(i, o) { var jo = $(o); if (jo.attr("datafield")) { var str = jo.val(); str = str.replace(" ", ""); if (str !== "") { data[jo.attr("datafield")] = jo.val(); } } }); return data; }
var save = function(sender) { $(sender).prop("disabled", true); //禁用按钮,防止重复发送 var data = getFormData("form1"); var jsonobj = { jsondata: data }; var textdata = JSON.stringify(jsonobj); $.ajax({ type: "POST", contentType: "application/json; charset=utf-8", url: "xxxxx.aspx/Save", dataType: "json", data: textdata, success: function(msg) { if (msg.d == "1") { document.form1.reset(); alert("保存成功!"); } else if (msg.d == "0") { alert("保存失败!"); } }, complete: function(jqXHR, textStatus) { $(sender).prop("disabled", false); //还原按钮 } }); }
string, which is simple and clean.
Then the client has collected the data, and the server should process the data. The key of the data we come from the front desk (json key) cannot all include all attributes of a certain data class. There are also many data classes, and only the server knows which class it is. So here we need to write a helper conversion class. There is another problem here. There may be many data classes. Do I have to write a method for each class? Isn’t that a pitfall? So we analyze the data format transmitted from the client to the server. It is a set of key-value pairs and does not repeat. It is equivalent to a DictionaryAfter talking about so much, post the core code
public static T1 UpdateObjectByDic<T1>(T1 scrobj, IDictionary<string, string> sourceobject, bool ignoreCase) where T1 : new() { T1 result = scrobj; PropertyInfo[] pifresults = typeof(T1).GetProperties(); foreach (var dic in sourceobject) { foreach (PropertyInfo pifresult in pifresults) { if (string.Compare(dic.Key, pifresult.Name, ignoreCase) == 0) { pifresult.SetValue(result, ChangeType(dic.Value, pifresult.PropertyType), null); break; } } } return result; } public static Object ChangeType(object value, Type targetType) { Type convertType = targetType; if (targetType.IsGenericType && targetType.GetGenericTypeDefinition().Equals(typeof(Nullable<>))) { NullableConverter nullableConverter = new NullableConverter(targetType); convertType = nullableConverter.UnderlyingType; } return Convert.ChangeType(value, convertType); }
[WebMethod(EnableSession = true)] public static string Save(Dictionary<string, string> jsondata) { string result = "0"; Model.Project pro = ConvertHandle.UpdateObjectByDic< Model.Project>(jsondata,new Model.Project,true); pro.CreatorID = BLL.Sys_User.GetCurUser().ID.ToString(); pro.CreatorName = BLL.Sys_User.GetCurUser().Name; prohandle.Insert(pro); result = "1"; return result; }
At the end of the article, this is just a simple application. Like the front-end collection I mentioned, many front-end js frameworks have already been developed, and the considerations are much more comprehensive than mine. The background processing is based on my This kind of front-end is simply collected, and many third-party frameworks have complete systems, but what I am talking about here is just a simple idea. When you don't have so many controls at the moment, can you implement this method simply? Of course, I strongly recommend not to reinvent the wheel, but you must understand the core function and principle of the wheel.
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!
Recommended reading:
How does ajax submit the form and implement file upload
How does ajax background success upload json data deal with
The above is the detailed content of Ajax form submission and background processing. For more information, please follow other related articles on the PHP Chinese website!