2014-01-24 35 views
0

我做了一个函数,其中有一个ajax调用,但得到响应后,它总是会去其他部分,即使得到的响应为“无效”。即使我在结果之后(在ajax本身)编写if else部分,但不能工作。请帮帮我。检查来自ajax的响应

var res=""; 
var urlString="/maintenance/calculate_GMT.jsp?full_date="+new_date+"&country="+country+"&time_zone="+time_zone; 

$.ajax({ 
    type: "GET", 
    cache :false, 
    async :false, 
    url: urlString, 
    success: function(result) { 
     res=result; 
    } 
}); 

alert("res=============>"+res); 
if(res=="invalid"){ 
    alert("Please enter the correct date time by checking the daylight savings time(DST)"); 
} else { 
    abc(res); 
} 

功能ABC:

function abc(result){ 
     document.getElementById("hide").value = result; 
     var data =document.getElementById("hide").value; 
     data=$.trim(data); 
     alert("data====>"+data); 
     var arr=data.split(" "); 
     alert(arr[0]); 
     alert(arr[1]); 
     $("#gmt_date").val($.trim(arr[0])); 
     $("#gmt_time").val($.trim(arr[1])); 
} 

回答

0

获取资源修剪数据

TRIM后

res=$.trim(res); 
0

您需要调整您的JavaScript是这样的...

$.ajax({ 
    type: "GET", 
     cache :false, 
     async :false, 
     url: urlString, 
     success: function(result) { 
      res=result; 

     alert("res=============>"+res); 
     if(res=="invalid"){ 
      alert("Please enter the correct date time by checking the daylight savings time(DST)"); 
      } 
      else { 
       abc(res); 
      } 

    } 
}); 
+0

我已经这样做了,但它总是在其他部分 – user2841408

+0

res参数的值是什么被返回的? – Jason

+0

我的错误水库与空间我已经修整res.now其工作正常谢谢你的帮助 – user2841408