Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
how do I pass string data containing html

"<table><tr><td>Time</td><td>greetings</td></tr><tr><td>morning</td><td>Good morning</td></tr></tr><tr><td>Day</td><td>Good afternoon</td></tr></table>" 

into my mvc via ajax post ?


What I have tried:

$.ajax({
           url: "/home/Receive",

           type: 'POST',
           data: data

       }).done(function (response) {
           if (response.length > 0) {
               console.log(response);
           }
           else {
               alert('Error saving comments. Please try again.');
           }
       });
Posted
Updated 27-Feb-18 16:52pm

1 solution

try

$.ajax({
    url: "/Home/Receive",
    type: 'POST',
    data: JSON.stringify({ 'htmlData': htmlData }),
    dataType: "json",
    traditional: true,
    contentType: "application/json; charset=utf-8",

}).done(function (response) {
 
Share this answer
 
Comments
istudent 27-Feb-18 22:55pm    
I just read about javascript injection attack. What would be the best approach to pass the html tags to action?
Karthik_Mahalingam 27-Feb-18 22:56pm    
this will pass as string data and using it for PDF generation, then there is no vulnerability.
Google : xss attack mvc
istudent 27-Feb-18 23:00pm    
this worked, but do i have to be worry ? my concern only
Karthik_Mahalingam 27-Feb-18 23:03pm    
unless you are treating the data as a string, there is no issue, if you are storing the data and retrieving later to populate in UI, then you will have to worry about xss

How to Handle Cross-Site Scripting in ASP.NET MVC Application?[^]
What is cross site script attack[^]
read these, you will get some idea, since yours is an ajax request so not much to worry.
istudent 27-Feb-18 23:03pm    
Thank you sir.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900