1

So I'm taking in data from an HTML form and then using AJAX to send the data to a web method to then be sent to a sqlite database, but my AJAX call is failing. What did I mess up? Am I doing it correctly?

HTML Form

<form id="addForm" >
     <input type="text"  name="playername" id="playername" placeholder="Player"/> 
     <input type="text" name="points" id="points" placeholder="Points" />
     <input type="text" name="steals" id="steals" placeholder="Steals" />
     <input type="text" name="blocks" id="blocks" placeholder="Blocks" /> 
     <input type="text" name="assists" id="assists" placeholder="Assists" />
     <input type="text" name="mpg" id="mpg" placeholder="MPG" /> 
     <input type="text" name="shotpct" id="shotpct" placeholder="Shot %" />
     <input type="text" name="threepct" id="3pct" placeholder="3 %" /> 
     <input type="button" value="add player" id="addbtn" name="addbtn" />
     </form>

AJAX

 $("#addbtn").click(function () {
                var form = $("#addForm").serializeArray();
                $.ajax({
                    type: 'POST',
                    url: "players.aspx/addRow",
                    data: JSON.stringify(form),
                    dataType: 'json',
                    success: function () {
                        alert('success');
                    },
                    error: function () {
                        alert('failure');
                    }
                });
                    });

and the web method(not finished, was just testing to see if I was getting data)

[WebMethod]
        public static void addRow(object form)
        {
            var stuff = form;
        }

I'm still learning how to use a lot of this stuff so any help will be greatly appreciated.

2 Answers 2

1

Replace

type: 'POST',

with

method: 'POST',

dataType: 'json' is not needed since you're not receiving data back. The data returned from the server, is formatted according to the dataType parameter.

Also remove JSON.stringify(form),this is already done with the .serialize();

Sign up to request clarification or add additional context in comments.

2 Comments

Yep. Thanks for your help!
How would I check that the data is being sent?
0

replace

JSON.stringify(form)

with

$('form').serializeArray()

So you will have:

$("#addbtn").click(function () {
                var form = $("form").serializeArray();
                $.ajax({
                    type: 'POST',
                    url: "players.aspx/addRow",
                    data: form,
                    dataType: 'json',
                    success: function () {
                        alert('success');
                    },
                    error: function () {
                        alert('failure');
                    }
                });
                    });

If you still get error. there might be a server side problem in the page you are calling. to make sure of that I suggest you use the 'Advanced REST client' witch is a Google Chrome extension and you can test posting values with it and see the result.

6 Comments

Still getting failure. I thought the "#addForm" was referring to the id of the html form?
yes you can use it as $('#addForm') too. I edited my answer please have look to the end of my answer.
I used the exact code you posted and I'm still getting the failure alert. I'll try the Advanced Rest Client.
I think my server side is working. It's status is "200: OK" when I test the POST, but like I said I don't know if I'm formatting things correctly.
oh after serializing I had forgotton to add it to your code check the edited answer
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.