这里要明白
for循环是同步的
ajax是异步的
所以for循环里面套ajax,for循环会先跑完再跑异步ajax
只需在ajax加上
async: false,
把异步改成同步
$.ajax({
url:'/esf/BlockAction_selectArea.jspx?path=' + overlays_path[i],
dataType: 'json',
async: false,
success:function(result) {
areaname = result["areaname"];
}
});
后台传json
public void selectArea(){
................
JSONObject json = new JSONObject();
json.put("areaname", block.getAreaname());
this.renderText(json.toString());
}
}
protected void renderText(String text) {
render(text, "text/plain;charset=UTF-8");
}
/**
* 封装response用于json输出
*
* @param text
* @param contentType
*/
protected void render(String text, String contentType) {
try {
getHttpServletResponse().setContentType(contentType);
getHttpServletResponse().getWriter().write(text);
} catch (IOException e) {
System.out.println(e);
}
}