title: How to send sentences, not single words, to an API and get them send back
body: <p>I am working on a project to convert an old Irish dictionary that uses non-standardised spelling. The objective is to send the non-standardised text to an API that will convert it to standardised spelling.
  The user should only have to click a button and the text will be sent behind the scenes to the API. The standardised spelling will return in two seconds and be seen on the webpage.</p>
  <p>It sounds easy but is surprisingly difficult. The code for the API is <a href="https://github.com/kscanne/caighdean" rel="nofollow noreferrer">here</a>
  The <a href="https://github.com/kscanne/caighdean/blob/master/API.md" rel="nofollow noreferrer">instructions</a> for setting it up is</p>
  <p>Here is code, both HTML and JavaScript, for running something similar:</p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>  window.addEventListener("load", function () {
      var form = document.getElementById("IGform");
      form.addEventListener("submit", function (event) {
        event.preventDefault();
        var XHR = new XMLHttpRequest();
  
        XHR.onreadystatechange = function(event) {
          if (XHR.readyState == XMLHttpRequest.DONE) {
            if (XHR.status == 200) {
              processPairs(JSON.parse(event.target.responseText));
            }
            else {
              var div=document.getElementById("output");
              div.innerHTML=XHR.status.toString()+': '+XHR.statusText;
            }
          }
        };
  
        XHR.open("POST", "https://cadhan.com/api/intergaelic/3.0");
        XHR.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        XHR.setRequestHeader('Accept','application/json');
        var payload = '';
        var radios = document.getElementsByName('foinse');
        for (var i=0; i&lt;radios.length; i++) {
          if (radios[i].checked) {
            payload = 'foinse='+radios[i].value+'&amp;teacs=';
            break;
          }
        }
        payload += encodeURIComponent(document.getElementById('sourcetext').value);
        XHR.send(payload);
        var div=document.getElementById("output");
        div.innerHTML='&lt;em&gt;Translating...&lt;/em&gt;'
      });
    });
  
    function processPairs(arr) {
      var div=document.getElementById("output");
      var suppress = true;
      div.innerHTML='';
      for(var i=0; i&lt;arr.length; i++) {
        t=arr[i][1];
        if (t=="\\n") {
          div.innerHTML+='&lt;br/&gt;';
          suppress = true;
        }
        else {
          if (suppress==false &amp;&amp; !t.match(/^[.,\/;”:!?%})]$/)) {
            div.innerHTML+=" ";
          }
          suppress = (t.match(/^[“\/$(\[#{]$/)!==null);
          div.innerHTML+=t;
        }
      }
    }</code></pre>
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;form id="IGform"&gt;
    &lt;textarea rows="10" cols="40" id="sourcetext" name="teacs" wrap="virtual"&gt;&lt;/textarea&gt;
    &lt;br/&gt;&lt;br/&gt;
    &lt;input type="submit" value="Submit"&gt;&lt;/input&gt;
    &lt;input type="reset" value="Clear"&gt;&lt;/input&gt;
    &lt;br/&gt;&lt;br/&gt;
    Source language:&lt;br/&gt;
    &lt;input type="radio" name="foinse" value="gd" checked&gt;Scottish Gaelic&lt;/input&gt;&lt;br/&gt;
    &lt;input type="radio" name="foinse" value="gv"&gt;Manx Gaelic&lt;/input&gt;&lt;br/&gt;
    &lt;input type="radio" name="foinse" value="ga"&gt;Pre-Standard Irish&lt;/input&gt;&lt;br/&gt;
  &lt;/form&gt;
  &lt;hr/&gt;
  &lt;div id="output"&gt;&lt;/div&gt;</code></pre>
  </div>
  </div>
  </p>
  <p>What I am trying to create instead of a form is a text that is already formatted in HTML. The user can click a button and the JavaScript will send the text to the API and then the standardised spelling will be provided pretty quickly thereafter.</p>
  <p>I hope that it is easy to understand. Any help with this matter would really appreciated.</p>