0

I have this Classic Asp code in header section of webpages:

<%
dim url, param, avgrate, votes, p, s
 url = "https://au2mailer.com/api/a2m-getschemaorg.asp"
 param = "?apikey=fe9fc289c3ff0af142b6d3bead98a923"
 Set HttpReq = Server.CreateObject("MSXML2.ServerXMLHTTP.6.0")
 HttpReq.SetOption(2) = 8192
 HttpReq.open "GET", url & param, false
 HttpReq.setRequestHeader "Content-Type", "application/json"
 HttpReq.Send()
 if (HttpReq.status = 200) Then
   response.write(HttpReq.responseText)
 end if
%>

and it works and is accepted by Google Schema Markup Validator. Page to check https://carmagic.dk/online-bil-forretning-hvordan.asp (Danish website) I need to change the code so that it works whether the page is html, asp, aspx or php and my idea was to change it to javascript. I have tried this javascript in header section

  <script>
      var request = new XMLHttpRequest();
        request.open('GET', 'https://au2mailer.com/api/a2m-getschemaorg.asp?apikey=fe9fc289c3ff0af142b6d3bead98a923');
        request.send();
        request.onload = ()=>{
          var receivedDom = new DOMParser().parseFromString(request.response, "text/html");
          var jsonstr = receivedDom.body.innerText;
          document.write(jsonstr);
        }
  </script>

The code execute but it does not work as the classic asp code! Is my classic asp code not possible in javascript?

  <p id="a2mjson"></p>
  <script>
      var request = new XMLHttpRequest();
        request.open('GET', 'https://au2mailer.com/api/a2m-getschemaorg.asp?apikey=fe9fc289c3ff0af142b6d3bead98a923');
        request.send();
        request.onload = ()=>{
          document.getElementById("a2mjson").innerHTML = request.response;
        }
  </script>

Using p tag works and make the script readable by schema validation tools but is it really necessary to use the p tag ?? seems amateurish??

Geert Bellekens
  • 12,788
  • 2
  • 23
  • 50
  • In what way does the code fail? What is the expected result and what is the observed result? – David Feb 18 '22 at 16:04
  • They are not "like for like", for starters you are not setting the `Content-Type` request HTTP header on your JavaScript XHR. – user692942 Feb 18 '22 at 21:37
  • Hi David, thanks for the comment. The expected result is that the search engines can read and approve the script that the api called returns. The classic asp code works and Google Schema Markup Validator can read it. My javascript cannot be read by Google Schema Markup Validator. I need a javascript because not all of our websites are .asp or .aspx – Michael Fiil Feb 19 '22 at 18:48
  • Hi user692942, thanks for the comment. My challenge is clearly that I have too little knowledge of java and I am not asking for a complete solution, but help with what I need to take a closer look at to make the javascript work. By the way, see my comment to David. – Michael Fiil Feb 19 '22 at 18:51
  • inserting an alert(request.response); after request.onload = ()=>{ gives the string I need to have inserted in the
    of each page
    – Michael Fiil Feb 23 '22 at 13:15

1 Answers1

0

You are not outputing the response header for JSON and the javascript is assuming it is a string. You have two options:

  1. Keep it as a string and use JSON.parse(), something like this:

    var request = new XMLHttpRequest();
    
    request.open('GET', 'https://au2mailer.com/api/a2m-getschemaorg.asp?apikey=fe9fc289c3ff0af142b6d3bead98a923');
    request.send();
    request.onload = ()=>{
        let res = JSON.parse(request.response);
        document.getElementById("a2mjson").innerHTML = res;
    }
    
  2. Change your response header in the ASP (recommended): See this answer here: How to return a JSON object in classic ASP

silver
  • 650
  • 4
  • 15