troubleshooting Question

Can any expert please help me take a stab at this?

Avatar of sammySeltzer
sammySeltzerFlag for United States of America asked on
AJAXASP.NETJSON
4 Comments1 Solution223 ViewsLast Modified:
I have several ajax calls that post data to the database using webmethods.

This works in terms of posting data to the databse.

Problem is that since there are several calls, once data is posted to the database, you see a ton of "Data Added Successfully" alerts.

User will then have to close each of those alerts.

This is both inefficient and extremely annoying to the users.

Is there a way to combine all the ajax calls into just one?

In other words, once a user clicks the submit button, only one Success alert is rendered instead of one for each ajax call?

Here is a small sample of the code that submits to the database and sends sends three alerts, one for each ajax call.

function getAllEmpData() {
                                var data = [];
                                $('tr.data-contact-personm').each(function () {
                                    var ename = $(this).find('.employeename01').val();
                                    var etitle = $(this).find('.employeetitle01').val();
                                    var email = $(this).find('.employeeemail01').val();
                                    var alldata = {
                                        'emplName': ename,
                                        'emplTitle': etitle,
                                        'empMail': email
                                    }
                                    data.push(alldata);
                                });
                                console.log(data);
                                return data;
                            }

                            function getAllSourcepData() {
                                var data = [];
                                $('tr.data-contact-person').each(function () {
                                    var sname = $(this).find('.sourcename01').val();
                                    var saddress = $(this).find('.sourceaddress01').val();
                                    var sincome = $(this).find('.sourceincome01').val();
                                    var alldata = {
                                        'mySource': sname,
                                        'mySAddress': saddress,
                                        'mySIncome': sincome
                                    }
                                    data.push(alldata);
                                });
                                console.log(data);
                                return data;
                            }

                            function getAllSpouseData() {
                                var data = [];
                                $('tr.data-contact-person2').each(function () {
                                    var spname = $(this).find('.spousename01').val();
                                    var spaddress = $(this).find('.spouseaddress01').val();
                                    var spincome = $(this).find('.spouseincome01').val();
                                    var alldata = {
                                        'mySpouse': spname,
                                        'mySPAddress': spaddress,
                                        'mySPIncome': spincome
                                    }
                                    data.push(alldata);
                                });
                                console.log(data);
                                return data;
                            }
                            function getAllDividentData() {
                                var data = [];
                                $('tr.data-contact-person3').each(function () {
                                    var divname = $(this).find('.dividentname01').val();
                                    var divaddress = $(this).find('.dividentaddress01').val();
                                    var divincome = $(this).find('.dividentincome01').val();
                                    var alldata = {
                                        'myDivName': divname,
                                        'myDivAddress': divaddress,
                                        'myDivIncome': divincome
                                    }
                                    data.push(alldata);
                                });
                                console.log(data);
                                return data;
                            }
                            function getAllReimbursedData() {
                                var data = [];
                                $('tr.data-contact-person4').each(function () {
                                    var reimname = $(this).find('.reimbursmentname01').val();
                                    var reimaddress = $(this).find('.reimbursmentaddress01').val();
                                    var reimincome = $(this).find('.reimbursmentincome01').val();
                                    var alldata = {
                                        'myReimbursName': reimname,
                                        'myReimbursAddress': reimaddress,
                                        'myReimbursIncome': reimincome
                                    }
                                    data.push(alldata);
                                });
                                console.log(data);
                                return data;
                            }
                            function getAllHonorariaData() {
                                var data = [];
                                $('tr.data-contact-person5').each(function () {
                                    var honorname = $(this).find('.inputHonoraria01').val();
                                    var alldata = {
                                        'myHonorname': honorname
                                    }
                                    data.push(alldata);
                                });
                                console.log(data);
                                return data;
                            }
                            function getAllGiftData() {
                                var data = [];
                                $('tr.data-contact-person6').each(function () {
                                    var gifname = $(this).find('.giftname01').val();
                                    var gifaddress = $(this).find('.giftaddress01').val();
                                    var gifincome = $(this).find('.giftincome01').val();
                                    var alldata = {
                                        'myGiftname': gifname,
                                        'myGiftaddress': gifaddress,
                                        'myGiftincome': gifincome
                                    }
                                    data.push(alldata);
                                });
                                console.log(data);
                                return data;
                            }
                            function getAllOrgData() {
                                var data = [];
                                $('tr.data-contact-person7').each(function () {
                                    var orgsname = $(this).find('.orgname01').val();
                                    var orgsaddress = $(this).find('.orgaddress01').val();
                                    var orgsincome = $(this).find('.orgincome01').val();
                                    var alldata = {
                                        'myOrgname': orgsname,
                                        'myOrgaddress': orgsaddress,
                                        'myOrgincome': orgsincome
                                    }
                                    data.push(alldata);
                                });
                                console.log(data);
                                return data;
                            }
                            function getAllCreditorData() {
                                var data = [];
                                $('tr.data-contact-person8').each(function () {
                                    var creditname = $(this).find('.creditorname01').val();
                                    var creditaddress = $(this).find('.creditoraddress01').val();
                                    var creditincome = $(this).find('.creditorincome01').val();
                                    var alldata = {
                                        'myCreditorname': creditname,
                                        'myCreditoraddress': creditaddress,
                                        'myCreditorincome': creditincome
                                    }
                                    data.push(alldata);
                                });
                                console.log(data);
                                return data;
                            }
                            $("#btnSubmit").click(function () {
                                var data = JSON.stringify(getAllEmpData());
                                console.log(data);
                                $.ajax({
                                    url: 'closures.aspx/SaveEmpData',
                                    type: 'POST',
                                    contentType: 'application/json; charset=utf-8',
                                    data: JSON.stringify({ 'empdata': data }),
                                    success: function () {
                                        alert("Data Added Successfully");
                                    },
                                    error: function (xhr, status, error) {
                                        alert(xhr.responseText);
                                    }
                                });
                            });
                            $("#btnSubmit").click(function () {
                                var data = JSON.stringify(getAllSourcepData());
                                console.log(data);
                                $.ajax({
                                    url: 'closures.aspx/SaveSourceData',
                                    type: 'POST',
                                    contentType: 'application/json; charset=utf-8',
                                    data: JSON.stringify({ 'empdata': data }),
                                    success: function () {
                                        alert("Data Added Successfully");
                                    },
                                    error: function (xhr, status, error) {
                                        alert(xhr.responseText);
                                    }
                                });
                            });
                            $("#btnSubmit").click(function () {
                                var data = JSON.stringify(getAllSpouseData());
                                console.log(data);
                                $.ajax({
                                    url: 'closures.aspx/SaveSpousData',
                                    type: 'POST',
                                    contentType: 'application/json; charset=utf-8',
                                    data: JSON.stringify({ 'empdata': data }),
                                    success: function () {
                                        alert("Data Added Successfully");
                                    },
                                    error: function (xhr, status, error) {
                                        alert(xhr.responseText);
                                    }
                                });
                            });
                            $("#btnSubmit").click(function () {
                                var data = JSON.stringify(getAllDividentData());
                                console.log(data);
                                $.ajax({
                                    url: 'closures.aspx/SaveDividentData',
                                    type: 'POST',
                                    contentType: 'application/json; charset=utf-8',
                                    data: JSON.stringify({ 'empdata': data }),
                                    success: function () {
                                        alert("Data Added Successfully");
                                    },
                                    error: function (xhr, status, error) {
                                        alert(xhr.responseText);
                                    }
                                });
                            });
                            $("#btnSubmit").click(function () {
                                var data = JSON.stringify(getAllReimbursedData());
                                console.log(data);
                                $.ajax({
                                    url: 'closures.aspx/SaveReimbursedData',
                                    type: 'POST',
                                    contentType: 'application/json; charset=utf-8',
                                    data: JSON.stringify({ 'empdata': data }),
                                    success: function () {
                                        alert("Data Added Successfully");
                                    },
                                    error: function (xhr, status, error) {
                                        alert(xhr.responseText);
                                    }
                                });
                            });
                            $("#btnSubmit").click(function () {
                                var data = JSON.stringify(getAllHonorariaData());
                                console.log(data);
                                $.ajax({
                                    url: 'closures.aspx/SaveHonorariaData',
                                    type: 'POST',
                                    contentType: 'application/json; charset=utf-8',
                                    data: JSON.stringify({ 'empdata': data }),
                                    success: function () {
                                        alert("Data Added Successfully");
                                    },
                                    error: function (xhr, status, error) {
                                        alert(xhr.responseText);
                                    }
                                });
                            });
                            $("#btnSubmit").click(function () {
                                var data = JSON.stringify(getAllGiftData());
                                console.log(data);
                                $.ajax({
                                    url: 'closures.aspx/SaveGiftData',
                                    type: 'POST',
                                    contentType: 'application/json; charset=utf-8',
                                    data: JSON.stringify({ 'empdata': data }),
                                    success: function () {
                                        alert("Data Added Successfully");
                                    },
                                    error: function (xhr, status, error) {
                                        alert(xhr.responseText);
                                    }
                                });
                            });
                            $("#btnSubmit").click(function () {
                                var data = JSON.stringify(getAllOrgData());
                                console.log(data);
                                $.ajax({
                                    url: 'closures.aspx/SaveOrgData',
                                    type: 'POST',
                                    contentType: 'application/json; charset=utf-8',
                                    data: JSON.stringify({ 'empdata': data }),
                                    success: function () {
                                        alert("Data Added Successfully");
                                    },
                                    error: function (xhr, status, error) {
                                        alert(xhr.responseText);
                                    }
                                });
                            });
                            $("#btnSubmit").click(function () {
                                var data = JSON.stringify(getAllCreditorData());
                                console.log(data);
                                $.ajax({
                                    url: 'closures.aspx/SaveCreditorData',
                                    type: 'POST',
                                    contentType: 'application/json; charset=utf-8',
                                    data: JSON.stringify({ 'empdata': data }),
                                    success: function () {
                                        alert("Data Added Successfully");
                                    },
                                    error: function (xhr, status, error) {
                                        alert(xhr.responseText);
                                    }
                                });
                            });
                        });
                     });
                  });
                });
               });
              });
             });
           });
        });
    </script>

And below is my attempt to combine the btnsubmit click into one alert.

This one does not work because it keeps giving an error that @parameters for each is required but not supplied.

This tells me there is something wrong with this approach but not sure how to resolve it.

Thanks for your help in advance.

                            function getAllSourcepData() {
                                var data = [];
                                $('tr.data-contact-person').each(function () {
                                    var sname = $(this).find('.sourcename01').val();
                                    var saddress = $(this).find('.sourceaddress01').val();
                                    var sincome = $(this).find('.sourceincome01').val();
                                    var alldata = {
                                        'mySource': sname,
                                        'mySAddress': saddress,
                                        'mySIncome': sincome
                                    }
                                    data.push(alldata);
                                });
                                console.log(data);
                                return data;
                            }

                            function getAllSpouseData() {
                                var data = [];
                                $('tr.data-contact-person2').each(function () {
                                    var spname = $(this).find('.spousename01').val();
                                    var spaddress = $(this).find('.spouseaddress01').val();
                                    var spincome = $(this).find('.spouseincome01').val();
                                    var alldata = {
                                        'mySpouse': spname,
                                        'mySPAddress': spaddress,
                                        'mySPIncome': spincome
                                    }
                                    data.push(alldata);
                                });
                                console.log(data);
                                return data;
                            }
                            function getAllDividentData() {
                                var data = [];
                                $('tr.data-contact-person3').each(function () {
                                    var divname = $(this).find('.dividentname01').val();
                                    var divaddress = $(this).find('.dividentaddress01').val();
                                    var divincome = $(this).find('.dividentincome01').val();
                                    var alldata = {
                                        'myDivName': divname,
                                        'myDivAddress': divaddress,
                                        'myDivIncome': divincome
                                    }
                                    data.push(alldata);
                                });
                                console.log(data);
                                return data;
                            }
    $("#btnSubmit").click(function () {
        var sourceComplete = false, spouseComplete = false, dividentComplete = false;
        function checkComplete() {
            if (sourceComplete && spouseComplete && dividentComplete) {
                $("#result").text("All complete");
            }
        }
        $("#result").text("");
        var data = JSON.stringify(getAllSourcepData());
        console.log(data);
        $.ajax({
            url: 'closures.aspx/SaveSourceData',
            type: 'POST',
            contentType: 'application/json; charset=utf-8',
            data: JSON.stringify({ 'empdata': data }),
            success: function () {
                sourceComplete = true;
                checkComplete();
            },
            error: function () {
                alert("Error while inserting data");
            }
        });
        var data = JSON.stringify(getAllSpouseData());
        console.log(data);
        $.ajax({
            url: 'closures.aspx/SaveSpousData',
            type: 'POST',
            contentType: 'application/json; charset=utf-8',
            data: JSON.stringify({ 'empdata': data }),
            success: function () {
                spouseComplete = true;
                checkComplete();
            },
            error: function () {
                alert("Error while inserting data");
            }
        });
        var data = JSON.stringify(getAllDividentData());
        console.log(data);
        $.ajax({
            url: 'closures.aspx/SaveDividentData',
            type: 'POST',
            contentType: 'application/json; charset=utf-8',
            data: JSON.stringify({ 'empdata': data }),
            success: function () {
                dividentComplete = true;
                checkComplete();
            },
            error: function () {
                alert("Error while inserting data");
            }
        });
    });
ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 1 Answer and 4 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 4 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros