mvc 5 / javascript post error

I have a modal div on a view, with a textbox and a button.  here is the javascript for the button click:

    $("#Search").on("click", function (e) {
        e.preventDefault();
        var address = $("#txtSa").val();
        var sa = address.replace(/ /g, "-");
        var frmData = new FormData();
        frmData.append("StreetAddress", sa);
        var url = "/Residents/SearchAddress"; ///" + sa;

        $.ajax({
            type: "POST",
            url: url,
            data: frmData,
            success: function (response) {
                alert(response);
            }
        });
    });

Open in new window


When this code executes I get the following script error:
jquery-3.1.1.js:8400 Uncaught TypeError: Illegal invocation
    at add (jquery-3.1.1.js:8400)
    at buildParams (jquery-3.1.1.js:8387)
    at Function.jQuery.param (jquery-3.1.1.js:8420)
    at Function.ajax (jquery-3.1.1.js:9010)
    at HTMLButtonElement.<anonymous> (Index:361)
    at HTMLButtonElement.dispatch (jquery-3.1.1.js:5201)
    at HTMLButtonElement.elemData.handle (jquery-3.1.1.js:5009)
add @ jquery-3.1.1.js:8400
buildParams @ jquery-3.1.1.js:8387
jQuery.param @ jquery-3.1.1.js:8420
ajax @ jquery-3.1.1.js:9010
(anonymous) @ Index:361
dispatch @ jquery-3.1.1.js:5201
elemData.handle @ jquery-3.1.1.js:5009

Open in new window


I am calling a controller action below, but it never reaches the controller:
[HttpPost]
        public ActionResult SearchAddress(string StreetAddress)
        {
            string result = "no";

            string address = StreetAddress.Replace("-", " ");

            Resident res = db.Residents.AsNoTracking().Where(r => r.StreetAddress.Equals(address)).FirstOrDefault();

            if (res != null)
            {
                result = "yes";
            }

            return Content(result);
        }

Open in new window


I don't know what I'm doing wrong here.
dan hendersonAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Julian HansenCommented:
Not sure why FormData is in the mix have you tried this
    $("#Search").on("click", function (e) {
        e.preventDefault();
        var address = $("#txtSa").val();
        var sa = address.replace(/ /g, "-");
        frmData = {StreetAddress : sa};
        var url = "/Residents/SearchAddress"; ///" + sa;

        $.ajax({
            type: "POST",
            url: url,
            data: frmData,
            success: function (response) {
                alert(response);
            }
        });

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Julian HansenCommented:
Just a note on client side data modification. There is not really much merit in doing this as good practice is to check the data on the server anyway - regardless of what you do on the client side as data can be modified before it is sent to the server.

The address therefore, can be sent raw from the client and go through the normal server side sanitizing.
0
dan hendersonAuthor Commented:
Thanks, Julian, that worked perfectly!  A couple things:
1.  I created a new FormData object because I had seen this used in previous examples.  Doing it your way in this instance was perfect.
2.  The modification to the address string is due to the fact that NOT replacing the spaces with '-' caused the string to be interperated in the controller as only the first part of string rather than the whole string ... I assume that this was due to character encoding issue.

Thanks for all your help on this.  I learn something every time I'm here!
0
Julian HansenCommented:
The modification to the address string is due to the fact that NOT replacing the spaces with '-' caused the string to be interperated in the controller as only the first part of string rather than the whole string ... I assume that this was due to character encoding issue.
I see you have commented out code where you were sending the data as a GET (part of the URL) - in that case it would create a problem with the controller.

However, when you POST the data this should not happen - your controller should be working off the URL and not posted data so you should be able to post the data in its raw format and then sanitize it in the controller.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.