How to send html content via ajax request?

I have a form with a text box (tinymce) for input content. When I execute an ajax request, I got an error:

Potentially dangerous Request.Form value was detected by the client

Then I tried something like

html.encodeURIComponent()or escape(), but the error is still here

HTML:

<form id="editForm" action="" method="post">
  <input type="text" id="title" name="title" />
  <textarea id="content" name="content"></textarea>
  <input type="button" id="submit" onclick="Submit();" />
</form>

Script (I use jQuery)

function Submit(){
 $.ajax({                
  url: 'ajax.aspx?type=addcontent&' + $('#editForm').serialize() + '&rnd=' + Math.random(),
  success: function(data) {
   alert('OK');
  }
 });
}

As soon as I press the submit button, an error will appear. The ajax request is not executed. I tried adding ValidateRequest="false"to the aspx page, but the problem is still here.

Any help is appreciated!

+5
source share
2 answers
$.post('/foo', { htmlContent: '<someHtml/>' }, function(result) {
    alert('ok');
});

script, HTML. ASP.NET , HTML. . ValidateRequest="false" aspx @Page.

<%@ Page Language="C#" AutoEventWireup="false" ValidateRequest="false" %>

ASP.NET MVC, , [ValidateInput(false)]:

[HttpPost]
[ValidateInput(false)]
public ActionResult Foo()
{
    ...    
}

, .NET 4.0, web.config:

<httpRuntime requestValidationMode="2.0" />
+7

, , base64- HTML. base64 jQuery:

var html = ...my html unsafe text...

var encodedHtml =  $.base64.encode(html);
+1

All Articles