0

This is my cart.php file. array i encoded to json and return to ajax function:

 $data = array();

     $data['total'] = '10000';
     $data['quantity'] = '10';

 echo json_encode($data);

This my index.php file. I create function ajax to return my array value in cart.php but the value is undefined:

 function view()
  {
    $.ajax({  
      url: 'cart.php',
      type:'POST',
      data:{action:'total'},
      datatype:'json',
      success: function (data) {
          
          alert(data.total);
       
      }
    });
  }
Nick is tired
  • 6,860
  • 20
  • 39
  • 51
  • It does not appear that the code you've shown could produce the error you're describing. Something else is going on, which we can't see from this information. – ADyson Jul 04 '21 at 08:47
  • Possible duplicate: https://stackoverflow.com/q/63589662/367456 – hakre Jul 04 '21 at 09:28

2 Answers2

2

Please change the "datatype" to "dataType", otherwise the system cannot know that the dataType is in JSON format

    $.ajax({  
      url: 'cart.php',
      type:'POST',
      
      // the next line seems unnecessary
      //data:{action:'total'},
      
      dataType:'json',
      success: function (data) {
        
        /// data.total will now be 10000
        alert(data.total);

      }
    });
Ken Lee
  • 6,985
  • 3
  • 10
  • 29
  • `datatype:'json'` ensures that jQuery will parse the JSON _before_ it reaches the success callback. This is described in the documentation. Therefore, the OP's issue is not a lack of parsing. – ADyson Jul 04 '21 at 08:46
  • 1
    @ADyson, on closer look, the root of the problem is the spelling of the word dataType (that's why when I previously add the JSON.parse it works). Note: `datatype:'json'` will NOT work, `dataType:'json'` works – Ken Lee Jul 04 '21 at 09:14
  • thank you bro , but you'r wrong the data type most be datatype not dataType , but your code run successfuly when data type like this datatype – Bithat Ch-Faiq Jul 04 '21 at 10:05
  • Sorry you're right it must be `dataType` for the automatic parsing to work. – ADyson Jul 04 '21 at 10:14
  • anyway thank you , but don't forget to change it to datatype smaller letter 't' not dataType 'T' – Bithat Ch-Faiq Jul 04 '21 at 12:44
  • @BithatCh-Faiq no that is incorrect, check the jQuery documentation: https://api.jquery.com/jquery.ajax/ . It must have large `T` in `dataType`, otherwise it will not automatically parse the data. – ADyson Jul 05 '21 at 04:27
  • @ADyson but in my code not working with Large 'T' – Bithat Ch-Faiq Jul 05 '21 at 10:03
  • @BithatCh-Faiq then something else must be the problem, have you checked for any JavaScript errors in that situation? Or checked that the raw output from your PHP is definitely valid JSON? "Not working" isn't a useful description of any problem, you need to do some debugging. P.s. why did you accept this answer then if it doesn't work for you? – ADyson Jul 05 '21 at 10:54
  • @ADyson i accepted because it work with small 't' and i told you it;s working but not with large 'T' anyway thank you – Bithat Ch-Faiq Jul 05 '21 at 17:10
  • @BithatCh-Faiq but the answer shows big T, as it should. Your situation sounds odd, something else must be wrong if it "works" with small t. – ADyson Jul 05 '21 at 20:55
  • If your code fails with capital T (I mean if you use **dataType** it will fail) --- Is it true that you have added the `data=JSON.parse(data)` line in your code which I have suggested in the first version of my answer ? (please note that I have removed this line in my final version of my answer) – Ken Lee Jul 06 '21 at 23:51
1
$.ajax({
   url: 'cart.php',
   type: 'POST',
   data:{action:'some action'},
   dataType:'JSON',
   cache:false,
   success:function(response){
        console.log(response)
   }
})

Just return the array as it is from the PHP. Ajax will automatically do the magic.

Muhammad Atif Akram
  • 1,204
  • 1
  • 4
  • 12