【ajax post提交数据】AJAX提交方式详解

时间:2016-12-17  来源:WebService  阅读:

ajax提交方式详解
在webform开发中,我们经常会用到ajax向后台提交数据,在我的公司,通常是提交到本页的后台去处理,或者是webservice,这两种方法都很简便,但是总显得略混乱。

今天在看blogengine的时候,发现它是这样处理的

首先用一个ajaxhelper.asp教程x页面处理全部的ajax请求,这个页面的后台全部是:

 

[webmethod]
        public static jsonresponse savepage(
            string id,
            string content,
            string title,
            string description,
            string keywords,
            string slug,
            bool isfrontpage,
            bool showinlist,
            bool ispublished,
            string parent)
        {
            webutils.checkrightsforadminpagespages(false);

            var response = new jsonresponse { success = false };
            var settings = blogsettings.instance;

            if (string.isnullorempty(id) && !security.isauthorizedto(rights.createnewpages))
            {
                response.message = "not authorized to create new pages.";
                return response;
            }


….

的webmethod,这样其实和webservice没有本质的区别,只是比webservice更加清爽

再看看前台是如何处理的:

var dto = {
                       "id": querystring("id"),
                       "content": content,
                       "title": title,
                       "desc": desc,
                       "slug": slug,
                       "tags": tags,
                       "author": author,
                       "ispublished": ispublished,
                       "hascommentsenabled": hascommentsenabled,
                       "cats": cats,
                       "date": date,
                       "time": time
                   };

                   //alert(json.stringify(dto));

                   $.ajax({
                       url: "../ajaxhelper.aspx/savepost",
                       type: "post",
                       datatype: "json",
                       contenttype: "application/json; charset=utf-8",
                       data: json.stringify(dto),
                       success: function (result) {
                           var rt = result.d;
                           if (rt.success) {
                               if (rt.data) {
                                   window.location.href = rt.data;
                               } else {
                                   showstatus("success", rt.message);
                               }
                           }
                           else
                               showstatus("warning", rt.message);
                       }
                   });

一个标准化的ajax提交处理机制,要比随意的滥用好的多

【ajax post提交数据】AJAX提交方式详解

http://m.bbyears.com/asp/29073.html

推荐访问:ajax提交数据 阻止ajax提交
相关阅读 猜你喜欢
本类排行 本类最新