0

Hii I had done below method in ajax , i have to insert monday,tuesday,wednesday like days but it inserts in non order sometimes it inserts monday first and sometimes it inserts thursday first please help me. i reffered many sites, All told to implement sleep is there any other way to do Thanks in advance

for (i = 1; i <= x; i++) {
                AvailDay = $.trim($('#day_' + i).text()).replace(/[\s\n\r]+/g, ' ');
                alert(AvailDay);
                AMFrom = $('#amfrom_' + i).val();
                AMTo = $('#amto_' + i).val();
                PMFrom = $('#pmfrom_' + i).val();
                PMTo = $('#pmto_' + i).val();
                $.ajax({
                    type: "POST",
                    url: "ProviderSignup.aspx/AddAvailibility",
                    data: "{'ProviderId':'" + parseInt(ProviderId) + "','AvailDay':'" + AvailDay + "','AMFrom':'" + AMFrom + "','AMTo':'" + AMTo + "','PMFrom':'" + PMFrom + "','PMTo':'" + PMTo + "'}",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (data) {
                    },
                    error: function () {
                        alert("Error");
                    }
                });

            }

And my C# Code

[WebMethod]
    public static Int32 AddAvailibility(Int32 ProviderId,string AvailDay,string AMFrom, string AMTo, string PMFrom,string PMTo)
    {
        string strconn = (string)ConfigurationSettings.AppSettings["ConnectionStringLocal"];
        List<Speaclity> Detail = new List<Speaclity>();
        string Success = string.Empty;
        string SelectString = "Insert Into T_Provider(ProviderId, AvailDay, AMFrom, AMTo, PMFrom, PMTo) values ('" + ProviderId + "','" + AvailDay + "','" + AMFrom + "','" + AMTo + "','" + PMFrom + "','" + PMTo + "') ";
        SqlConnection cn = new SqlConnection(strconn);
        cn.Open();
        SqlCommand cmd = new SqlCommand(SelectString, cn);
        int result = cmd.ExecuteNonQuery();

        if (result != 0)
        {
            Success = "True";
        }
        else
        {
            Success = "False";
        }
        return result;
    }
  • aside from SQL injection and stringified non-referred booleans : what is supposed to be monday - or thursday for that matter - how do you create your client-side form? Even better, why don't you create a mockup (where the ajax-call never gets actually fired) in a jsfiddle? – Leon Jan 27 '15 at 07:44
  • @Leon i cant understand can u post example – Rajesh Shanmugam Jan 27 '15 at 07:55
  • Since Ajax is asynchronous, you will get the calls in an inconsistent order. You could set async:false, but I would take a look at queuing up your Ajax calls. Check out [this question](http://stackoverflow.com/questions/10980997/make-jquery-ajax-calls-in-order) for suggestions on how you could do this. – abbottmw Jan 27 '15 at 08:34

1 Answers1

0

$.ajax(...) is async by default. Try please async: "false"

$.ajax({
                async: false,
                type: "POST",
                url: "ProviderSignup.aspx/AddAvailibility",
                data: "{'ProviderId':'" + parseInt(ProviderId) + "','AvailDay':'" + AvailDay + "','AMFrom':'" + AMFrom + "','AMTo':'" + AMTo + "','PMFrom':'" + PMFrom + "','PMTo':'" + PMTo + "'}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (data) {
                },
                error: function () {
                    alert("Error");
                }
            });

In this case js function will be wait result before send next request.

Dmitresky
  • 536
  • 8
  • 21