Submit an HTML element using jQuery Ajax

Take a look at the code below.

    $("input").change(function (eventArgs)
    {
        $.ajax(
        {
            type: "POST",
            url: "/MyWebSite/TestServices.asmx/StoreValue",
            data: eventArgs.target,
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: TestSessionCallback,
            error: GeneralCallbackFailure
        });
    });

Open in new window


This causes my browser to lock up and crash. Is there a way to send an HTML element via a jQuery Ajax call like this?

Or... can an entire form be submitted?
LVL 22
Russ SuterSenior Software DeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.

skijCommented:
Try using JSON.stringify() because you are sending JSON.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
<script type="text/javascript">
 $(document).ready(function () {
     $("input").change(function (eventArgs)
     {
         var name = escape($(this).attr('name'));
         var data = escape($(this).val());
         $.ajax(
         {
             type: "POST",
             url: "/MyWebSite/TestServices.asmx/StoreValue",
             data: JSON.stringify(new function(){ this[name] = data; }, null, '\t'),
             contentType: "application/json; charset=utf-8",
             dataType: "json",
             success: TestSessionCallback,
             error: GeneralCallbackFailure
         });
     });
 });

 function TestSessionCallback() {
 }

 function GeneralCallbackFailure() {
 }

</script>
</head>
<body>
<form name="theForm" id="theForm" method="post" target="/">
First Name: <input type="text" name="billingFName" id="billingFName" />
<br />
Last Name: <input type="text" name="billingLName" id="billingLName" />
</form>
</body>
</html>

Open in new window

Russ SuterSenior Software DeveloperAuthor Commented:
Still just locks up. Hmmm...
skijCommented:
Have you tried using the code exactly as I posted it above?  Does it lock up for you when you try that code?
Introduction to Web Design

Develop a strong foundation and understanding of web design by learning HTML, CSS, and additional tools to help you develop your own website.

Russ SuterSenior Software DeveloperAuthor Commented:
Yes and yes. Any other suggestions?
skijCommented:
What browser are you using that locks up and crashes?  Does it work in any other browsers?  

What response code is the server returning in the header, and what data is the server returning?
Russ SuterSenior Software DeveloperAuthor Commented:
Chrome is my primary browser. It doesn't work on any other I've tried.

There is no response. That's kinda the definition of "locking up"
skijCommented:
The problem is probably on the server end.  Do you know what response code is the server returning in the header, and what data is the server returning?  Perhaps the server isn't returning anything.
Russ SuterSenior Software DeveloperAuthor Commented:
it's not getting to the server. The browser is LOCKING UP
skijCommented:
Try this code, exactly as posted, and post exactly what the response is in the alert after you change a form value.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
<script type="text/javascript">
 $(document).ready(function () {
     $("input").change(function (eventArgs)
     {
         var name = escape($(this).attr('name'));
         var data = escape($(this).val());
         $.ajax(
         {
             type: "POST",
             url: "/MyWebSite/TestServices.asmx/StoreValue",
             data: JSON.stringify(new function(){ this[name] = data; }, null, '\t'),
             contentType: "application/json; charset=utf-8",
             dataType: "json",
             success: function(data) {alert(data);TestSessionCallback();},
             error: function(XMLHttpRequest, textStatus, errorThrown) {alert(errorThrown);GeneralCallbackFailure();} 
         });
     });
 });

 function TestSessionCallback() {
   alert("Success");
 }

 function GeneralCallbackFailure() {
   alert("Error");
 }

</script>
</head>
<body>
<form name="theForm" id="theForm" method="post" target="/">
First Name: <input type="text" name="billingFName" id="billingFName" />
<br />
Last Name: <input type="text" name="billingLName" id="billingLName" />
</form>
</body>
</html>

Open in new window

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
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
jQuery

From novice to tech pro — start learning today.