简体   繁体   中英

How to get specific ajax result in codeigniter

I am not really into jquery that's why I am getting hard to get the result. I want to get the specific result of the ajax. The result of the ajax is like this:

{"success":1,"result":[{"id":"7","cal_events_name":"Day Off","cal_events_image":"\/assets\/img\/3_copy_copy1.png"}]}
{"success":1,"result":[{"id":"8","cal_events_name":"Normal Day","cal_events_image":"\/assets\/img\/2_copy1.png"}]}

How can i get only the value of cal_events_image ? Take note there are multiple result. Should I use .each function for that? Thank you guys in advance.

You can try this:

let response={"success":1,"result":[{"id":"7","cal_events_name":"Day Off","cal_events_image":"\/assets\/img\/3_copy_copy1.png"}]};
//response=JSON.parse(response);  // Use this line, if the response you get is in string format
alert(response["result"][0]["cal_events_name"]);

You need to parse it using json. something like this...

success: function(response) {
var jsonData = JSON.parse(response);
   for (var i = 0; i < jsonData.result.length; i++) {
     var counter = jsonData.result[i];
     console.log(counter.cal_events_image);
   }
}

Then it will display the result according to it's value.

here is the working example: js Fiddle

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM