将div数据从js传递给aspx.cs webmethod

时间:2012-12-24 11:32:47

标签: asp.net jquery

当我检查checkbox时,我想将div中的数据发送到webmethod,该Webservice写在aspx codebehind中if ($(this).is(':checked')) { if (editorContent != "") { if (!($("#divCustomTemplateContent1:contains('<html>')"))) { editorContent = "<html><head></head><body>" + editorContent + "</body></html>"; } $.ajax({ type: "POST", url: "3.aspx/AddorRemoveViewInBrowser", contentType: "application/json; charset=utf-8", data: "{'html':'" + encodeURIComponent(editorContent) + "',isAddorRemove:'add'}", dataType: "json", success: function (returndata) { alert(returndata.d); $('#divCustomTemplateContent1').html(returndata.d); } }); } } 内。我的js是这样的:

<table bgcolor="#8da2b2" border="0" cellpadding="0" cellspacing="0" width="100%">
    <tbody>
        <tr>
            <td width="100%" align="center">
                <table border="0" cellpadding="0" cellspacing="0" width="581">
                    <tbody>
                        <tr>
                            <td bgcolor="#ffffff" align="center" width="581">
                                <table bgcolor="#ffffff" border="0" cellpadding="0" cellspacing="0" style="margin-top: 20px;" width="581">
                                    <tbody>
                                        <tr>
                                            <td bgcolor="#b73224" height="110">
                                                <table border="0" cellpadding="0" cellspacing="0" style="width: 581px;">
                                                    <tbody>
                                                        <tr>
                                                            <td width="581" height="110" align="left">
                                                                <img src="/App_Themes/Main/Images/Ec/TemplateImages/3s_header.jpg" border="0" alt="XYZ Company" width="581" height="110">
                                                            </td>
                                                        </tr>
                                                    </tbody>
                                                </table>
                                            </td>
                                        </tr>
                                    </tbody>
                                </table>
                                <table border="0" cellpadding="0" cellspacing="0" width="550">
                                    <tbody>
                                        <tr>
                                            <td align="left" valign="top" class="mainbar" width="550">
                                                <table border="0" cellpadding="0" cellspacing="0" width="550">
                                                    <tbody>
                                                        <tr>
                                                            <td style="font-family: Tahoma, Arial; font-size: 12px; color: #000000; padding-top: 15px;" valign="top" align="left">
                                                                <h2 style="font-size: 18px; border-left: 10px solid #a02f27; padding-top: 0; padding-right: 0;
                                                                    padding-bottom: 0; padding-left: 7px; margin: 0px;">
                                                                    Lorem ipsum dolor sit amet conse ct etuer adipiscing elit</h2>
                                                                <p style="color: #333333; margin-top: 10px; margin-left: 0; margin-bottom: 20px;
                                                                    margin-right: 0;">
                                                                    Donec imperdiet, nibh sit amet pharetra placerat, tortor purus condimentum lectushttp://tinyurl.com/d5rel77,
                                                                    at dignissim nibh velit vitae sem. Nunc <a href="http://some-site.com/" style="color: #344692;">
                                                                        condimentum blandit</a> tortorphasellus facilisis neque vitae purus.</p>
                                                                <p style="color: #333333; margin-top: 0; margin-left: 0; margin-bottom: 20px; margin-right: 0;">
                                                                    Aliquam facilisis nisl in nisi. Ut ultricies massa eget est. Donec eget orci eget
                                                                    urna aliquam egestas. Nulla vitae felis. <a href="http://some-site.com/" style="color: #344692;">
                                                                        Maecenas bibendum</a>, nunc eu aliquet ultricies, massa massa aliquet est, nec
                                                                    dignissim nisl ante eget lectus.</p>
                                                                <p style="margin-top: 0; margin-left: 0; margin-bottom: 5px; margin-right: 0; width: 100%;
                                                                    text-align: right; padding: 0;">
                                                                    <a href="#top" style="color: #344692; font-size: 11px; margin-top: 0; margin-left: 4px;
                                                                        margin-bottom: 0; margin-right: 0;">Back to top</a></p>
                                                                <img src="/App_Themes/Main/Images/Ec/TemplateImages/hr-big.gif" border="0" alt="hr" width="550" height="10" style="display: block;">
                                                            </td>
                                                        </tr>
                                                    </tbody>
                                                </table>
                                                <table border="0" cellpadding="0" cellspacing="0" width="551">
                                                    <tbody>
                                                        <tr>
                                                            <td style="font-family: Tahoma, Arial; font-size: 12px; color: #000000; padding-top: 15px;" valign="top" align="left">
                                                                <h2 style="font-family: Arial; font-size: 18px; border-left: 10px solid #a02f27;
                                                                    padding-top: 0; padding-right: 0; padding-bottom: 0; padding-left: 7px; margin: 0px;">
                                                                    Fermentum quam donec imperde lorem ipsum dolar amet consect</h2>
                                                                <p style="color: #333333; margin-top: 15px; margin-left: 0; margin-bottom: 20px;
                                                                    margin-right: 0;">
                                                                    <img src="/App_Themes/Main/Images/Ec/TemplateImages/main-content-inline-small.jpg" border="0" alt="Main Content Inline Small" hspace="10" width="147" height="147" align="left">Aliquam facilisis nisl in nisi. Ut ultricies massa eget est.
                                                                    Donec eget orci eget urna aliquam egestas. Nulla vitae felis. Maecenas bibendum,
                                                                    nunc eu aliquet ultricies, massa massa aliquet est, nec dignissim nisl ante eget
                                                                    lectus.Aliquam facilisis nisl in nisi. Ut ultricies massa eget est. Donec eget orci
                                                                    eget urna aliquam egestas. Nulla vitae felis. Maecenas bibendum, nunc eu aliquet
                                                                    ultricies, massa massa aliquet est, nec dignissim nisl ante eget lectus.%%%%last_name%%%%</p>
                                                                <p style="margin-top: 0; margin-left: 0; margin-bottom: 5px; margin-right: 0; width: 100%;
                                                                    text-align: right; padding: 0;">
                                                                    <a href="#top" style="color: #344692; font-size: 11px; margin-top: 0; margin-left: 4px;
                                                                        margin-bottom: 0; margin-right: 0;">Back to top</a></p>
                                                                <img src="/App_Themes/Main/Images/Ec/TemplateImages/hr-big.gif" border="0" alt="hr" width="550" height="10" style="display: block;">
                                                            </td>
                                                        </tr>
                                                    </tbody>
                                                </table>
                                            </td>
                                        </tr>
                                    </tbody>
                                </table>
                                <table bgcolor="#8da2b2" border="0" cellpadding="0" cellspacing="0" width="581">
                                    <tbody>
                                        <tr>
                                            <td style="font-family: Arial; font-size: 11px; font-weight: normal; color: #333333;" align="center" valign="bottom" height="100">
                                                <p style="margin-top: 0; margin-left: 0; margin-bottom: 20px; margin-right: 0; text-align: center;">
                                                    XYZ Company and the XYZ Company Logo are registered trademarks of <span>XYZ Company
                                                        Corp</span>.<br>
                                                    XYZ Company Corp - 123 Some Street, City, ST 99999. ph +1 4 1477 89 745</p>
                                            </td>
                                        </tr>
                                    </tbody>
                                </table>
                            </td>
                        </tr>
                    </tbody>
                </table>
            </td>
        </tr>
    </tbody>
</table>

这里我的问题是div中的数据是这样的

  [System.Web.Services.WebMethod]
        public static string AddorRemoveViewInBrowser(string html, string isAddorRemove)
        {
            string returnHtml = string.Empty;
            if (isAddorRemove == "add")
            {
                CampaignManager ocampaignmanger = new CampaignManager();
                returnHtml = ocampaignmanger.AddViewInbrowser(html);
            }
            else
            {
                CampaignManager ocampaignmanger = new CampaignManager();
                returnHtml = ocampaignmanger.RemoveViewInbrowser(html);
            }
            return returnHtml;
        }

然后网络方法根本没有被调用。当我为div提供简单的数据时,那么就没有错误了。

{{1}}

问题是什么?

1 个答案:

答案 0 :(得分:0)

首先,您需要知道自己已将contentTypedataType指定为json。因此,在您的情况下,发送到服务器的数据以及从服务器发回的数据只能是json数据。

做这样的改变。

$.ajax({
    type: "POST",
    ....
    data: {'html':encodeURIComponent(editorContent),'isAddorRemove':'add'},
    ....
});

其次,您正在从需要使用JavaScriptSerializer or JsonConvert.SerializeObject(json.NET)

的服务器发送字符串对象