簡體   English   中英

Converting classic asp to javascript: How to write or make returned string (json+ld) visible in header of webpage using javascript?

[英]Converting classic asp to javascript: How to write or make returned string (json+ld) visible in header of webpage using javascript?

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??

您沒有為 JSON 輸出響應 header 並且 javascript 假設它是 ZB45CFFE0845DD3D20D1ZBEE728。 你有兩個選擇:

  1. 將其保留為 string 並使用 JSON.parse(),如下所示:

     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. 在 ASP 中更改您的響應 header(推薦):在此處查看此答案: 如何在經典 ASP 中返回 JSON object

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM