How to get Json by Ajax in . html?

Asked

Viewed 486 times

3

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

1 answer

4


Use the 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>

  • Thank you so much, you can’t imagine how much you’ve helped me! It worked perfectly, but you could explain your code a little bit more so I can understand well?

  • @Jedaiasrodrigues ready :D

  • you are wild, thank you very much even once again, but there is only one last problem. The question of timing, I need to return the obtained values. See this code for example I need to present the results here, you could help me one more time?

  • @Jedaiasrodrigues I made a code to return exactly the buttons you need to show http://pastebin.com/EPvzC5V7

  • 1

    Thank you very much guy even for your help. What was missing she just set the ajax as synchronous: $.ajaxSetup({&#xA; async : false&#xA; })

Browser other questions tagged

You are not signed in. Login or sign up in order to post.