2012-12-24 51 views
1

當我檢查checkbox時,我想將div中的數據發送到webmethod,這是在Webservice內以aspx代碼隱藏的方式寫入的。我的JS是這樣的:將div數據從js傳遞到aspx.cs webmethod

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); 
         } 
        }); 
       } 
} 

這裏我的問題是當DIV的數據是這樣的

<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> 

然後將WebMethod不獲取被調用。當我給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; 
     } 

是什麼問題?

+0

你肯定不叫發送字符串對象爲純?你有沒有調試過,看到沒有命中'AddorRemoveViewInBrowser'? – Aristos

+0

是的,我已經看到與調試。我想我需要編碼或js​​on的數據的div ...但我不知道sytax – user1551433

回答

0

首先您需要知道您已指定contentTypedataTypejson。所以在你的情況下,發送到服務器並從服務器發回的數據只能是json數據。

確實是這樣改變的。

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

其次,你從你需要使用服務器JavaScriptSerializer or JsonConvert.SerializeObject(json.NET)