2013-12-15 210 views
0

我有发送多个Ajax请求的代码此行WCF发送多个Ajax请求web服务

$(".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) { 



          } 
         }); 


        } 

       }); 

该web服务的操作更新该分贝。 由于jQuery的Ajax是工作不同步它的发送,在错误结束服务器multipal要求:“试图在了一个操作不是一个套接字”

我变薄,这是因为数据库正在试图每次打开新的连接。

任何想法如何以同步的方式在数组上循环?

感谢

Baaroz

+1

你应该重新考虑你的逻辑,只使用一个Ajax请求 –

+0

可能加入“异步:假”只需发送所有提交的数据将jQuery AJAX? – baaroz

+1

这将是一个修复,但最糟糕的 –

回答

0

相反,这样做同步,其可以锁定客户端浏览器(见this post),为什么不使用递归发送请求 - 即当一个请求完成发送下一个请求:

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); 
+0

我喜欢它人感谢很多 – baaroz