Wilson Yeoh
Wilson Yeoh

Reputation: 3

Ajax submit and get invalid character error

I am using ASP.NET MVC, When I tried to key in <HTML> in text field and submit form it prompt me parse error and invalid character.

But when I tried to key in <222> or HTML>, the form submitted without problem.

It is weird and I tried to encode it before submitting but I still get the same result, anyone have an idea?

Below is the code:

$('#form1').bind('submit', function (e) {                
    e.preventDefault(); 
    $(this).ajaxSubmit({
        iframe: true,
        dataType: 'json',
        error: function (e) {

        },
        success: function (data) {

        }

Here are the form submit

@using (Html.BeginForm("TestSet", "test", FormMethod.Post, new { enctype = "multipart/form-data", id = "form1" }))

Here are the controller

   [AcceptVerbs(HttpVerbs.Post)]
    public FileUploadJsonResult test(testViewModel testvm)
    {
    }

I did tested to

  $('#form1').bind('submit', function (e) {                
    e.preventDefault(); 
     var data = $("#frmTaskSet").serializeArray();
    $(this).ajaxSubmit({
        iframe: true,
        dataType: 'json',
         //data: JSON.stringify(data),
        or 
        //data: encodeURIComponent(data), 
        error: function (e) {

        },
        success: function (data) {

        }  

Update: I had shrink the problem area, I try to key in <ABC> or <ABC into the text field and pass this value to controller then it hit the error but weird thing is if I pass using ABC> or <123> it successful without problem.

           var task1 = $("#text1").val().toString();
           var encode1 =  escapeHTML(task1);
           var encode2 =  encodeURIComponent(task1);
           var encode3 = $("<div>").text(task1).html();


            alert(encode1); //&lt;ABC&gt;
            alert(encode2); //%3CABC%3E
            alert(encode3); //&lt;ABC&gt;

                $(this).ajaxSubmit({
                    iframe: true,
                    dataType: 'json',
                     data: { Name: encode4 },
                   //contentType: "application/json",
                    error: function (e,errordata, errorObject) {

                    },

ERROR I get is parse error , and syntaxError:Invalid Character, it happen before I post to the server side.

Upvotes: 0

Views: 1134

Answers (1)

Golda
Golda

Reputation: 3891

The ASP.Net MVC does not support user to enter html tags. To achieve this you can use ValidateInput attribute and AllowHtml attribute.

You can use this at controller level or action level

Example

[ValidateInput(false)]
public class HomeController : Controller
{
 public ActionResult Index()
 {
 return View();
 }         
 // Do your code
}

Also you can use the AllowHtml attribute for particular property

public class Demo
{
 [Required]
 [Display(Name = "Title")]
 public string Title{ get; set; }

 [AllowHtml]
 [Required]
 [Display(Name = "Description")]
 public string Description{ get; set; }
} 

For more information visit this article

Upvotes: 1

Related Questions