Geeks With Blogs

Mindfully <> Creative Put your mind to it

After banging my head for days on a “A potentially dangerous Request.Form value was detected" issue when post (ajax-ing) a form in ASP.NET MVC 2 on .NET 4.0 framework using jQuery and CKEditor, I found that when you use the following:

Code Snippet
  1. $.ajax({
  2.     url: '/TheArea/Root/Add',
  3.     type: 'POST',
  4.     data: $("#form0Add").serialize(),
  5.     dataType: 'json',
  6.     //contentType: 'application/json; charset=utf-8',
  7.     beforeSend: function ()
  8.     {
  9.         pageNotify("NotifyMsgContentDiv", "MsgDefaultDiv", '<img src="/Content/images/content/icons/busy.gif" /> Adding post, please wait...', 300, "", true);
  10.         $("#btnAddSubmit").val("Please wait...").addClass("button-disabled").attr("disabled", "disabled");
  11.     },
  12.     success: function (data)
  13.     {
  14.         $("#btnAddSubmit").val("Add New Post").removeClass("button-disabled").removeAttr('disabled');
  15.         redirectToUrl("/Exhibitions");
  16.     },
  17.     error: function ()
  18.     {
  19.         pageNotify("NotifyMsgContentDiv", "MsgErrorDiv", '<img src="/Content/images/content/icons/cross.png" /> Could not add post. Please try again or contact your web administrator.', 6000, "normal");
  20.         $("#btnAddSubmit").val("Add New Post").removeClass("button-disabled").removeAttr('disabled');
  21.     }
  22. });


Notice the following:

Code Snippet
  1. data: $("#form0Add").serialize(),


You may run into the “A potentially dangerous Request.Form value was detected" issue with this. One of the requirements was NOT to disable ValidateRequest (ValidateRequest=”false”). For this project (and any other project) I felt it wasn’t necessary to disable ValidateRequest.

Note: I’ve search for alternatives for the posting issue and everyone and their mothers continually suggested to disable ValidateRequest. That bothers me – a LOT. So, disabling ValidateRequest is totally out of the question (and always will be). 

So I thought to modify how the “data: “ gets serialized. the ajax data fix was simple, add a .html(). YES!!! IT WORKS!!! No more “potentially dangerous” issue, ajax form posts (and does it beautifully)! So if you’re using jQuery to $.ajax() a form with CKEditor, remember to do:

Code Snippet
  1. data: $("#form0Add").serialize().html(),


or bad things will happen. Also, don’t forget to set

Code Snippet
  1. config.htmlEncodeOutput = true;


for the CKEditor config.js file (or equivalent). Example:

Code Snippet
  1. CKEDITOR.editorConfig = function( config )
  2. {
  3.     // Define changes to default configuration here. For example:
  4.     // config.language = 'fr';
  5.     config.uiColor = '#ccddff';
  6.     config.width = 640;
  7.     config.ignoreEmptyParagraph = true;
  8.     config.resize_enabled = false;
  9.     config.skin = 'kama';
  10.     config.enterMode = CKEDITOR.ENTER_BR;
  11.  
  12.     config.toolbar = 'MyToolbar';
  13.     config.toolbar_MyToolbar =
  14.     [
  15.         ['Bold', 'Italic', 'Underline'],
  16.         ['JustifyLeft', 'JustifyCenter', 'JustifyRight', 'JustifyBlock', 'Font', 'FontSize', 'TextColor', 'BGColor'],
  17.         ['BulletedList', 'NumberedList', '-', 'Outdent', 'Indent'],
  18.         '/',
  19.         ['Scayt', '-', 'Cut', 'Copy', 'Paste', 'Find'],
  20.         ['Undo', 'Redo'],
  21.         ['Link', 'Unlink', 'Anchor', 'Image', 'Flash', 'HorizontalRule'],
  22.         ['Table'],
  23.         ['Preview', 'Source']
  24.     ];
  25.     config.htmlEncodeOutput = true;
  26. };


Happy coding!!!

Tags:

Posted on Friday, March 19, 2010 3:54 AM | Back to top


Comments on this post: Using jQuery, CKEditor, AJAX in ASP.NET MVC 2

# re: Using jQuery, CKEditor, AJAX in ASP.NET MVC 2
Requesting Gravatar...
Thank you for the comment!
Left by Ray Linder on Apr 15, 2010 3:11 AM

# re: Using jQuery, CKEditor, AJAX in ASP.NET MVC 2
Requesting Gravatar...
can you give us a sample mvc project with using ckeditor ?
Left by sefer kılıç on Apr 20, 2010 2:47 PM

# re: Using jQuery, CKEditor, AJAX in ASP.NET MVC 2
Requesting Gravatar...
Love the content

zayıflama
Left by zayıflama on May 17, 2010 12:12 PM

# re: Using jQuery, CKEditor, AJAX in ASP.NET MVC 2
Requesting Gravatar...
Loves the article
Left by jersey on Jun 06, 2010 9:48 PM

# re: Using jQuery, CKEditor, AJAX in ASP.NET MVC 2
Requesting Gravatar...
thanks you, you have resolve my problem
"A potentially dangerous Request.Form value was detected"
simply with include "config.htmlEncodeOutput = true;"
in the config.js af ckeditor.
great job.
Left by nicola on Jul 13, 2010 5:58 PM

# re: Using jQuery, CKEditor, AJAX in ASP.NET MVC 2
Requesting Gravatar...
ops...
now i see the html tag like <p>some text</p>
after to decode with server.htmldecode of asp.net...
have tou the solution?
thanks
Left by nicola on Jul 13, 2010 6:21 PM

# re: Using jQuery, CKEditor, AJAX in ASP.NET MVC 2
Requesting Gravatar...
This solution doesn't work for me. Looking in to it, the serialize method returns string so .html() can't be called after it.

Not sure what the code's doing in your case but it's certainly not working for me.
Left by Tristan on Aug 25, 2010 3:29 AM

# re: Using jQuery, CKEditor, AJAX in ASP.NET MVC 2
Requesting Gravatar...
Is it possible to create form through Ckeditor?
student loan
Left by Christina on Nov 03, 2010 1:43 AM

# re: Using jQuery, CKEditor, AJAX in ASP.NET MVC 2
Requesting Gravatar...
Best part fo this article is A potentially dangerous Request.Form value was detected" issue when post (ajax-ing) a form in ASP.NET MVC 2 on .NET 4.0 framework using jQuery and CKEditor. virtual pet
Left by atlas on Dec 11, 2010 6:01 AM

# re: Using jQuery, CKEditor, AJAX in ASP.NET MVC 2
Requesting Gravatar...
I would give a five out of five rating to it. Need some more posts like this to be coming out Thank you and looking for more of this kind. grants
Left by atlas on Dec 13, 2010 2:00 PM

# re: Using jQuery, CKEditor, AJAX in ASP.NET MVC 2
Requesting Gravatar...
Thank you for the share.
Left by Cloud Cone on Dec 25, 2010 11:04 PM

# re: Using jQuery, CKEditor, AJAX in ASP.NET MVC 2
Requesting Gravatar...
I just cant stop reading this.  Its so cool, so full of information that I just didnt know.  Im glad to see that people are actually writing about this issue in such a smart way, showing us all different sides to it.  Youre a great blogger.  Please keep it up. NEC & Toshiba VoIP telephone systems in New York and New Jersey

Left by atlas on Dec 27, 2010 10:33 AM

# Nice
Requesting Gravatar...
I just stumbled upon your blog and wanted to say that I have really Uncorked Ventures Wine Club enjoyed reading your blog posts. Thanks for the post.
Left by ralf madson on Jan 08, 2011 12:08 AM

# re: Using jQuery, CKEditor, AJAX in ASP.NET MVC 2
Requesting Gravatar...
I have seen something revealing after a very long time. multivers software | multivers

Left by adail on Jan 15, 2011 4:32 AM

# custom logo design
Requesting Gravatar...
I just stumbled upon your blog and wanted to say that I have really Satellite internet in Tennessee enjoyed reading your blog posts. Thanks for the post.
Left by William fallin on Jan 18, 2011 10:51 PM

# re: Using jQuery, CKEditor, AJAX in ASP.NET MVC 2
Requesting Gravatar...
I wanted to thank you for this great read!! I definitely enjoying every little bit of it and I have you bookmarked to check out new stuff you post. Contact some state or county agencies in your state to get in contact with some facilities that may be places of interest to you.
Cool post very informative I just found your site and read through a few posts although this is my first comment,contact lenses overnight
i'll be including it in my favorites and visit again for sure .
Left by contactlensline. on Jan 19, 2011 12:36 PM

# re: Using jQuery, CKEditor, AJAX in ASP.NET MVC 2
Requesting Gravatar...
I wanted to thank you for this great read!! I definitely enjoying every little bit of it and I have you bookmarked to check out new stuff you post. Contact some state or county agencies in your state to get in contact with some facilities that may be places of interest to you.
Left by Wall Street Journal Wine Club on Jan 26, 2011 10:01 PM

# re: Using jQuery, CKEditor, AJAX in ASP.NET MVC 2
Requesting Gravatar...
Thanks really helped me out.
Left by Bob on Feb 01, 2011 5:06 AM

# re: Using jQuery, CKEditor, AJAX in ASP.NET MVC 2
Requesting Gravatar...
u can't miss it. Plus there are many provincial signes for directions (the blue ones) once you're on road
Left by plumber fresno on Feb 09, 2011 12:47 AM

# re: Using jQuery, CKEditor, AJAX in ASP.NET MVC 2
Requesting Gravatar...
Intimately, the post is really the freshest on this laudable topic. I bookmarked this blog a while ago because of the useful content and I am never being disappointed. Keep up the good work.
Left by Genital Herpes in Men on Feb 16, 2011 5:27 AM

# re: Using jQuery, CKEditor, AJAX in ASP.NET MVC 2
Requesting Gravatar...
h is precisely why I’m going to take time to do so now. Maybe things are different on the no-doubt sprawling Nintendo
Left by plumber frisco on Feb 17, 2011 4:42 AM

# re: Using jQuery, CKEditor, AJAX in ASP.NET MVC 2
Requesting Gravatar...
Nice post.I really liked it and gonna link it on my home page Chin Acne to shear with my friends.
Thanks.
Left by abir on Feb 17, 2011 10:55 PM

# Thanks
Requesting Gravatar...
I simply needed to thank you very much all over again. I do not know the things I might have sorted out without those information contributed by you concerning that theme. It seemed to be a real distressing problem in my position, however , being able to view your expert avenue you handled it took me to weep for contentment. I am happier for this advice and in addition sincerely hope you know what a powerful job you were carrying out instructing people today with the aid of your site. Most likely you have never met any of us.
Left by Scholarship essays on Feb 18, 2011 1:00 AM

# re: Using jQuery, CKEditor, AJAX in ASP.NET MVC 2
Requesting Gravatar...
I like this type of article very much.This is that type of article which gives us proper idea about this topic and helps us to increase our knowledge level about of it.Now I am going to link it on my home page Best over the counter acne treatment to share with my friends.
Thanks!
Left by abir ahmed on Feb 25, 2011 12:01 AM

# hi
Requesting Gravatar...
I like this article very much as it's written on the basis of research.From this article we came to know various things of this subject. Now I am share my web link Best over the counter acne treatment with my friends.
Thanks to all
Left by abir ahmed on Feb 26, 2011 11:38 AM

# re: Using jQuery, CKEditor, AJAX in ASP.NET MVC 2
Requesting Gravatar...
It’s really a great post..I would like to appreciate your work and I am going to recommend it to my friends.Thanks for sharing.
Left by money claims online on Mar 08, 2011 3:46 AM

# re: Using jQuery, CKEditor, AJAX in ASP.NET MVC 2
Requesting Gravatar...
issue, ajax form posts (and does it beautifully)! So if you’re using jQuery to $.ajax() a form with CKEditor, remember to d Government Grants For Debt Relief
Left by Government Grants For Debt Relie on Mar 13, 2011 2:19 PM

# re: Using jQuery, CKEditor, AJAX in ASP.NET MVC 2
Requesting Gravatar...
Custom pocket folders provide an excellent way to organize your presentations, press kits and other promotional materials in a professional manner. Pocket Folder Printing that includes full color and a gloss coating will make the best impression.
Left by Pocket Folder Printing on Mar 14, 2011 1:23 PM

Comments have been closed on this topic.
Copyright © Ray Linder | Powered by: GeeksWithBlogs.net