Ignore error in json from jQuery

0

I have the following problem when I make a call

var googleAPI = "https://www.googleapis.com/books/v1/volumes/"+item.id;
$.ajax({ 
    type: 'GET', 
    url: googleAPI, 
    data: { pub : 'value' }, 
    dataType: 'json',
    success: function (item) { 
                    titulo.value = item.volumeInfo.title;
                    autor.value = item.volumeInfo.authors; 
                    reseña.value = item.volumeInfo.description;  
                    categoria.value =  item.volumeInfo.categories;
                    año.value = item.volumeInfo.publishedDate;
                    idioma.value = item.volumeInfo.language;
                    num_paginas.value = item.volumeInfo.pageCount; 
    },
    error: function(libro, status, jqXHR) {
    }  
});

when they are all I have no problems, but sometimes the "item.volumeInfo.authors" does not exist in some books and lets you keep changing the others. It stops. If the field existed but it was empty, I could do something ... but since there is NO I do not know what to do, since I do not have much knowledge. I do not know if you understand? Greetings .-

    
asked by Luis Alberto Cárdenas Vargas 04.08.2017 в 22:31
source

2 answers

0

If you are sure that the volumeInfo object exists, you can use this to skip reference errors produced when an object does not have a certain property:

autor.value = item.volumeInfo.authors || false;

Keep in mind that this only works to reference properties of objects that exist. If the object does not exist, it will throw you an error.

    
answered by 04.08.2017 / 22:36
source
0

You could read the results dynamically using each .

I have allowed myself to modify the code, since success has been declared obsolete. jQuery recommends handling Ajax requests using done, fail, always . See here .

The code shows two possibilities:

  • Show in the HTML the key of the json object of the answer and the value of each key next to it (this is built dynamically, regardless of what is in the json).
  • Obtain the id of each element and show it next to possible ids that you would have in the HTML for each element (it would work dynamically, only that you would have each manual element id in the HTML and those that do not come in certain requests would be in white). I think the 1st possibility is more flexible.
  • From these two examples, I believe that everything else is possible.

    The URL I use is a functional github, only for demonstration purposes.

    I hope it serves you.

    $.ajax({
      type: 'GET',
      url: 'https://api.github.com',
      dataType: 'json',
    
    })
       .done(function( data ) {
     
           //Asignación dinámica
            $.each(data, function(k, v) {
                $("#result").append("<strong>" + k + "</strong>: " +v + "<br>"); 
            });
     
           //Asignación por id
            $.each(data, function(k, v) {
                $("#"+k).append(v + "<br>"); 
            });
        })
    
        .fail(function( xhr, status, errorThrown ) {
                alert( "Sorry, there was a problem!" );
                console.log( "Error: " + errorThrown );
                console.log( "Status: " + status );
                console.dir( xhr );
         })
    
        .always(function( xhr, status ) {
                alert( "La llamada se ha completado!" );
         });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    
    <body>
    
    <div id="result"></div>
    <hr />
    <p>Ejemplo de asignación por id:</p>
    
    <p id="current_user_url"></p>
    </body>
        
    answered by 04.08.2017 в 23:59