Error 500 in ajax when serialize input within a form containing "<" character

Viewed 220

I have a form to save/edit users and passwords into a database. During some tests I discovered that when the user or password text contains the char "<" the ajax call fails with error 500. I googled a lot but I didn't find anything useful to solve the problem. To be honest I'm not very skilled in jQuery and Ajax programming.

This is my ajax code, I use ASP.NET MVC5 and JQuery 3.3.1

        var mydata = $("#FormItem").serialize();
        $.ajax({
            dataType: "text",
            type: "POST",
            url: '@Url.Action("SaveUsPwdInDB", "Computer")',
            data: mydata,
            success: function (result) {
                var jobj = JSON.parse(result);
                if (jobj.isValid == false) {
                    alert("Save Failed: \n\n" + jobj.jserror);
                } else {
                    alert("Save Successful !");
                }
            },
            error: function (result) {
                fnPopupErrors(result);
            }
        });

I tried to change serialization type using .serializeArray() and also dataType from text to Json, but I got always the same 500 error. What I noticed is that when the code reaches the data:mydata row it throws the error, never reaches the execution of code in "SaveUsPwdInDB".

Any idea?

1 Answers

OK after digging all the comments and suggestion I found a solution, perhaps not the most secure one, but it correct the problem.

Following the link suggested by @RoyMcCrossan (thanks a lot !) stackoverflow.com/q/3621272/519413, I added the [AllowHtml] to the definition in the model, something like this:

    public class BlogEntry {
       public int UserId {get;set;}
       
       [AllowHtml] 
       public string BlogText {get;set;}
    }

and everything started working like a charm !

Many thanks to all contributors !

Related