Get JavaScript content

0

I need to make some changes to a web page.

I'm creating an HTML form with JavaScript and need help in the following case:

My form has a field where the user types the ID of a SPOJ-WWW contest, after typing the ID the page should make a get of some contest data that is in the SPOJ to complete other fields of my form.

For example, the user types the ID of some problem and the page automatically identifies what the problem is and completes the fields, title, description, goals, etc.

Is there any function in JavaScript that does this in some way, type giving content get?

    
asked by anonymous 19.04.2018 / 15:13

2 answers

0

Making a GET request using pure javascript:

var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
      document.getElementById("demo").innerHTML =
      this.responseText;
    }
  };
  xhttp.open("GET", "ajax_info.txt", true);
  xhttp.send();

Example taken from W3schools

You can still use the fetch function as mentioned by Caio Saldanha's response.

Placing a request with the jQuery library:

$.get( "ajax/test.html", function( data ) {
  $( ".result" ).html( data );
  alert( "Load was performed." );
});

Example taken from the jQuery documentation

    
19.04.2018 / 19:23
0

I do not know if I understood correctly, because I do not know what SPOJ rsrsrs .... But I believe that what you want can be done in the following way:

1 - Implement a function to manipulate the ID padding. This function will fire when the onfocusout event occurs in the input.

2 - When firing, the function fetches the endpoint that serves the content you want.

3 - You get the Response from the GET Request made (by fetch ()) and then put the received information in the fields you decide on.

handleIdSearch(event){
 let valor = event.target.value;
 fetch("http://urlDaApi.com.br?idProduto="+valor).then( function(resposta){
      // Veio um objeto do tipo Response, precisamos transformá-lo em JSON
      // Response.json() retorna outra Promise
      return resposta.json();
   }).then( function(json){
      // Pegue os valores do JSON
      document.getElementById("outroInput").value = json.valorOutroInput;
   }).catch( function(error){
      throw error;
   });
}

document.getElementById("campoIdProduto").addEventListener('focusout', 
// Callback do evento
function(event){
    handleIdSearch(event);
})

You go there!

    
19.04.2018 / 19:07