0

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

1
  • could you please post associated HTML too..?
    – vijayP
    Dec 2, 2015 at 4:31

3 Answers 3

1

Tell me if this worked for you.

success: function(dataone){
                  var parsedJson = $.parseJSON(dataone);
                  var totalscore = parsedJson.totalscore;
                  var totalwicket = parsedJson.totalwicket
     }
0
1

If the response is in the form of string then you need to parse it using JSON.parse else you can read keys from object(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'];
0

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
           }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.