Getting information from a JSON and saving in SQL Server using JavaScript

0

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

Now I would need to save this information in a SQL Server database, how would that be possible?

It's been really hard 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><divclass="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>
    
asked by anonymous 22.10.2018 / 23:40

1 answer

1

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

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

See these answers in SO EN :

  

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

Máttheus Spoo well remembered in the comments, that if the question's author is already using JavaScript, a good option code on the server side would also be Node.js .

Node.js is a cross-platform, open-source JavaScript interpreter built from the Google Chrome V8 JavaScript engine, whose purpose is, in short, to execute JavaScript code outside of a browser, so 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 primarily used to build Web servers.

With Node.js you could then write server-side code, with JavaScript, to write data to SQL Server. See this answer in SO EN :

  

node.js - send Data to SQL DataBase with nodeJs - Stack Overflow

Sources:

  

Node.js - Wikipedia
Node.js

    
23.10.2018 / 01:35