0

I have this lines of code that send multiple ajax request to wcf

$(".cWarpO").each(function () {

                  if ($(this).find(".newId").length > 0) {
                        counter++;

                        var Mapping = new Array();
                        Mapping[0] = counter;
                        Mapping[1] = $(this).find(".idN").html(); //new id
                        Mapping[2] = $(this).find(".idO").html();
                        Mapping[3] = newCourseId;
                        Mapping[4] = courseOldId;
                        Mapping[5] = isGenric;
                        Mapping[6] = oldGenricCourse;

                        $.ajax({
                            url: "/WebServices/general.svc/mappingCourses",
                            type: "POST",
                            data: JSON.stringify({ Mapping: Mapping }),
                            dataType: "json",
                            contentType: "application/json; charset=utf-8",
                            success: function (data) {



                            }
                        });


                    }

                });

the webservice operation is update the db. Since the jquery ajax is work unsynchronized it's sending the server multipal request that end in error:"An operation was attempted on something that is not a socket"

I thins it's because the database is trying to open new connection every time.

Any idea how to loop on the array in a synchronized way?

Thanks

Baaroz

baaroz
  • 19,119
  • 7
  • 37
  • 47

1 Answers1

0

Instead of doing it synchronously which may lock the clients browser (see this post), why don't use send the requests recursively - ie whenever a request finishes send the next request:

var mapCourse = function($ele, index) {
    if(index < $ele.length) {
            //stuff
            $.ajax({
                url: "/WebServices/general.svc/mappingCourses",
                type: "POST",
                data: JSON.stringify({ Mapping: Mapping }),
                dataType: "json",
                contentType: "application/json; charset=utf-8",//you don't need to set this
                success: function (data) {
                    //stuff

                    mapCourse($ele, index + 1);
                }
            });
        }
    }
}

mapCourse($(".cWarpO"), 0);
Community
  • 1
  • 1
megawac
  • 10,953
  • 5
  • 40
  • 61