Request Ajax jquery Jsonp Returning Undefined

1

I need to handle the feedback of the following code below. Remembering that it works correctly however, at some point, the searched term does not exist returning "undefined" and does not show anything on the screen. I need you to look for a standard, locally saved image right now. Thank you in advance.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script><scripttype="text/javascript">

$.ajax({  
type: "GET",
url: "http://alvoparabusca.com.br", 
data: {termo:"termo da busca",limitado:1,tipo:"imagem"},

success: function(data){
    img = data["resultado"][0].imagem;
    $("html").css({"background-image":"url("+ img +")"});
},
dataType: "jsonp"
});

</script>
    
asked by anonymous 12.04.2016 / 14:34

1 answer

0
success: function(data) {
  if (data && data.resultCount > 0) {
    img = data["resultado"][0].imagem;
    $("html").css({
      "background-image": "url(" + img + ")"
    });
  } else { //Se não for localizado, seta sua imagem default
    $("html").css({
      "background-image": "url(suaImagemDefault.jpg)"
    });
  }
}

Would that be?

    
12.04.2016 / 14:47