ITeye博客

Jquery ajax获取返回值问题

tree3170 · 2012-08-28 09:17 · Jquery · JS · Web前端 · Markdown

这个问题我晚上弄了一晚上,然后一直没弄出来,不知是什么问题,今天早上上网一搜,竟然给找到了答案 ,欣喜啊  呵呵 。现在把问题描述一下:

就是我判断一个函数里面的值是否存在,通过jquery  ajax调用,但是在调用函数的时候,因为$.ajax()默认是异步的,所以它会在还没有来得及赋值的

情况下,就去执行其他步骤,导致程序一直执行错误,也就是当我保存没有包含已存在的configid时,它也会执行return  false,导致保存没有效果,

而包含已存在configid时会弹出已存在消息,这个是正确的.现在贴一段代码:

调用函数:

 

[javascript] view plaincopy

  1. function save(){
  2. var configID=$("select[name=configId_extentionID]").val();
  3. if(!checkValidInput()){
  4. return false;
  5. }
  6. //test the configid whether it exsited
  7. if(!IsConfigIDExist(configID)){
  8. return false;
  9. }
  10. //window.location=xpcHomeLocation+'PlugInManageAction.do?operation=save';
  11. document.PlugInForm.action=xpcHomeLocation+'PlugInManageAction.do?operation=save';
  12. document.PlugInForm.method='post';
  13. document.PlugInForm.submit();
  14. }

[javascript] view plaincopy

  1. function IsConfigIDExist(configID){
  2. var b=true;
  3. $.ajax({
  4. url:xpcHomeLocation+'PlugInManageAction.do?operation=checkConfigIDExist&configID='+configID,
  5. type:"POST",
  6. async:false,//synchronize:it must have been returned the result ,then execute the program.
  7. dataType:"json",
  8. success:function(result){
  9. if(result.success==false){
  10. alert(result.msg);
  11. b=false;
  12. }
  13. }
  14. })
  15. return b;
  16. }

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。