javascript - how to extract string list values in ajax success - Stack Overflow

admin2025-04-22  0

In controller

 List<String> currectTime = userService1.getUserCurrectTime(time,hash);  

I need to extract each value from the ajax success responce.

$( ".dropdownlist" ).change(function() {
    var time=$('#dropdownlist').val();
    var hash=$('#hash').val();
    alert(time+hash);

    var savedata = {    
        time:time,
        hash:hash
    }; 

    $.ajax({
        type:'POST',
        url:'../test',
        data:savedata,    
        error:function() {
            alert("error");
        },
        success:function(data) {
            alert(data);
            var obj = JSON.parse(data);
        }
    });  
});  

If I alert the response data I see the below

["28-12-2014 01:10:45 NST","30-12-2014 02:40:35 NST","06-11-2014 16:10:45 NST"]

How can I get these values separately.

In controller

 List<String> currectTime = userService1.getUserCurrectTime(time,hash);  

I need to extract each value from the ajax success responce.

$( ".dropdownlist" ).change(function() {
    var time=$('#dropdownlist').val();
    var hash=$('#hash').val();
    alert(time+hash);

    var savedata = {    
        time:time,
        hash:hash
    }; 

    $.ajax({
        type:'POST',
        url:'../test',
        data:savedata,    
        error:function() {
            alert("error");
        },
        success:function(data) {
            alert(data);
            var obj = JSON.parse(data);
        }
    });  
});  

If I alert the response data I see the below

["28-12-2014 01:10:45 NST","30-12-2014 02:40:35 NST","06-11-2014 16:10:45 NST"]

How can I get these values separately.

Share Improve this question edited Dec 19, 2018 at 0:37 vinS 1,4755 gold badges25 silver badges39 bronze badges asked Dec 17, 2014 at 11:15 Priyanka ShajuPriyanka Shaju 7992 gold badges12 silver badges23 bronze badges 2
  • 1 $.each(obj, function(index, value){console.log(value)}) method would be useful or fixed index like obj[0] – Girish Commented Dec 17, 2014 at 11:18
  • I don't understand if you need to loop the object or retrieve the date, time and tz separately – Razorphyn Commented Dec 17, 2014 at 11:19
Add a ment  | 

2 Answers 2

Reset to default 2

Simply loop over your object.

var obj = JSON.parse(data);

for (i = 0; i < obj.length; i++) { 
    console.log(obj[i]);
}
$.each(obj, function(i,data){
    alert(data);

});
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1745289851a294617.html

最新回复(0)