Taking information from a JSON and saving it to SQL Server using Javascript

Asked

Viewed 437 times

0

I have this code, which returns the information I want and shows on the page.

Now I would need to save this information to an SQL Server database, as this would be possible?

This is very difficult for me, since I’ve never worked with API, JSON or Javascript before.

function makeServiceCall() {					
  var url = "http://widsservicedev.yaharasoftware.com/WidsService/JSON/GetPortagePrograms/?apikey=104043F0-9C24-4957-879D-046868973CC4&callback";
  
  $.getJSON(url, function (data) {
    //var myArray = [];
    //myArray[0] = data;
    parseProgramData(data, url);
  });					
}

function parseProgramData(jsonData, url) {		
  $("#dataHeader").empty();
  $("#dataHeader").append('<b>' + url + '</b>');
  
  var programUL = document.getElementById("programUL");		

  for (var pgmIndex = 0; pgmIndex < jsonData.Programs.length; pgmIndex++) {					
    var pgmLi = document.createElement("li");
    var program = jsonData.Programs[pgmIndex];
    var programInfoRevision = program.ProgramInfoRevisions[0];
    var numberTitle = programInfoRevision.ProgramNumber + " " + programInfoRevision.ProgramTitle;
    pgmLi.appendChild(document.createTextNode(numberTitle));
    programUL.appendChild(pgmLi);					
    
    var linebreak = document.createElement("br");
    pgmLi.appendChild(linebreak);
    
    var poLabel = document.createElement("label");
    poLabel.appendChild(document.createTextNode("Program Outcomes"));
    poLabel.classList.add("headerLabel");					
    pgmLi.appendChild(poLabel);					
    
    var pgmOutcomeUL = document.createElement("UL");
    pgmLi.appendChild(pgmOutcomeUL);
    
    for (var poIndex = 0; poIndex < program.ProgramOutcomes.length; poIndex++) {					
      var poLi = document.createElement("li");
      poLi.appendChild(document.createTextNode(program.ProgramOutcomes[poIndex].Description));
      pgmOutcomeUL.appendChild(poLi);
    }
  }
}
.bodyFrame {
  margin: 40px;
}

.headerLabel {
  font-weight: bold;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<html>
  <body>
    <div class="bodyFrame">
      <h2 style="text-align:center;">WIDS JSON Retrieval Example</h2>
      
      <button type="button" onclick="makeServiceCall()">Retrieve JSON Data</button>
      <br /><br />
            
      <label class="headerLabel">Programs</label>
      <ul id="programUL"></ul>
    <div>
  </body>

  <footer>		
  </footer>
</html>

  • I don’t have much knowledge in Javascript, actually this is the first project that I use javascript... I have more knowledge in Java , would be possible something in Java to solve this problem?

1 answer

1

In this case, as in most cases, Javascript is running on client side, in the browser of the user accessing your website, more specifically.

To save data to SQL Server you would need to have some code running on server side, like ASP.NET or PHP, for example.

See these answers in the OSEN:

How to save html form data to sql db using javascript or jquery - Stack Overflow
javascript - How to use Knockoutjs to save data into SQL database? - Stack Overflow

Editing

The Máttheus Spoo recalled well, in the comments, that if the author of the question is already using Javascript, a good server-side code option would also be the Node.js.

Node.js is a cross-platform, open-source Javascript code interpreter built from Google Chrome’s Javascript V8 engine, whose purpose, briefly, is to run Javascript code outside of a browser, therefore, much used to create server-side scripts.

Node.js has an event-oriented asynchronous architecture and is designed to create scalable network applications, and is mainly used to build web servers.

With Node.js it would then be possible to write server-side code, with Javascript, to write data to SQL Server. See this response in the OSEN:

Node.js - send Data to SQL Database with nodejs - Stack Overflow

Sources:

Node.js - Wikipedia
Node.js

  • 1

    Since he is trying to do javascript and already has some knowledge about javascript, it would be worth pointing out Node.js, no?

  • Good @Máttheusspoo, I’ll add your tip in the reply!

Browser other questions tagged

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