How to get Json for Ajax in .html?

3

Suppose I have a .html file that I just created in notepad with a Ajax code where I want to get the data generated by this application . I made a code here, and when I put a breakpoint in the application I realize that the request is arriving normally, but after that the Ajax error message is generated, what could it be? Would anyone have an example to get the data from the cited link?

    
asked by anonymous 08.06.2015 / 00:23

1 answer

4

Use getJson :

$.getJSON( "http://horariofacil.azurewebsites.net/Mobile/ObterCursos", function(data) {
  var items = [];

  // each é pra iterar uma lista de dados
  $.each( data, function( key, val ) { 

    // key é o indice (0, 1, ..)
    // val é o objeto, no seu caso o curso

    // push adiciona o valor ao array
    items.push( "<li id='" + val.Codigo + "'>" + val.Nome + "</li>" );

  });
    
  // crio um elemento <ul>
  $( "<ul/>", {

    // função join transforma o array em uma string
    // html: seria o conteudo do <ul> 
    html: items.join( "" ) 

  }).appendTo( "body" ); // appendTo vai adicionar o <ul> ao <body>
 });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    
08.06.2015 / 00:35