i am new in jquery and ajax. I am really cant figured it out how can i show the array result send from the controller from json_encode. Below is the code in controller:
Controller
$totaldata['data'] = array('totalscore' =>$totalscore,'totalwicket' =>$totalwicket,'totalover' =>$totalover,'totalextra' =>$totalextra);
$this->output->set_output(json_encode($totaldata));
Jquery
$.ajax({
url: 'xxxxxxxxx',
type : 'POST',
data:{
xxxx : arrayValue,
id : matchid,
},
success: function(dataone){
//$('.addmatchsuccess').html(dataone).show();
//setTimeout(function() {location.reload() },3000);
}
});
Problem I want to get each array value to be placed in a input field in view ...which i couldnt figured out still how it gonna work. Please any help will be appreciated. Thank You
Tell me if this worked for you.
success: function(dataone){
var parsedJson = $.parseJSON(dataone);
var totalscore = parsedJson.totalscore;
var totalwicket = parsedJson.totalwicket
}
If the response is in the form of
string
then you need to parse it usingJSON.parse
else you can readkeys
fromobject
(dataone)
Try this:
if (typeOf dataone === 'string') {
myData = JSON.parse(dataone)['data'];
} else {
myData = dataone['data'];
}
var totalscore = myData['totalscore'];;
var totalwicket = myData['totalwicket'];
var totalover = myData['totalover'];
var totalextra = myData['totalextra'];
You can do this:
success: function(dataone){
var json_response= eval('('+response+')');
var totalscore =json_response.totalscore
var totalwicket =json_response.totalwicket
var totalover = json_response.totalover
var totalextra = json_response.totalextra
}