Home > Back-end >  AJAX response doesn't print in console
AJAX response doesn't print in console

Time:10-24

I am using AJAX to get form data from Django application and I want to print the response in the console.

    $.ajax({ 
    type: 'GET' ,               
    url: url,              
    data: {'PUITS': PUITSId },
    dataType: "json",
    success: function (response){
        console.log(response)
        var  response = JSON.stringify(response);
        var  response = JSON.parse(response);
        console.log(response.PUITS);
        console.log(response.DUSE);
        console.log(response.CS);

The first console.log(response) gives me the following data:

[{"model": "measure.surveillancedespuits", "pk": 15, "fields": {"PUITS": "1", "DATE_TEST": "2021-09-10", "MODE": "GL", "CS": "1", "SITUATION": "O", "DUSE": "27", "PRES_TBG": "27", "PRES_CSG": "27", "PRES_AVD": "27", "RESEAU_GL": "27", "ANNULAIRE_TECH": "27", "OBSERVATION": "Nothing", "Controle_Pression_ENSP": true, "Test_Puits": false, "Controle_Pression_DP": false, "post_date": "2021-09-10T08:56:16.864Z", "author": "smail"}}]

and I want to print also some individual data from fields like PUITS, DATE_TEST, post_date` in the console but it shows an error!.

CodePudding user response:

according to your log result, your response is an array not an object. you CANT access array elements using dot(.) operator.

your response array contains only one element at index 0.

const object = response[0]
object.fields.PUITS
  • Related