AJAX提交方式詳解

來源:互聯網
上載者:User

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提交處理機制,要比隨意的濫用好的多

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.