简体   繁体   English

将参数传递给GET-Request

[英]Pass parameters to GET-Request

G'morning folks, 早上好,

I just have a small problem to fix this issue: 我只有一个小问题可以解决此问题:

I have a page which should GET some data from a url when opening and the display in the own content. 我有一个页面,该页面在打开并在自己的内容中显示时应该从URL获取一些数据。 But the GET url contains Parameters from my url. 但是GET网址包含我网址中的参数。

So: 1. Get Parameters from my URL like www.mydomain.com?test=1&test1=bla 2. open GET with this parameters (1 and bla) and display result 因此:1.从我的URL中获取参数,例如www.mydomain.com?test=1&test1=bla 2.使用此参数(1和bla)打开GET并显示结果

Here my current version: 这是我当前的版本:

<body>
  <h3>Visa Informationen</h3>
  <p id="data"></p>
  <script>
    var xmlhttp = new XMLHttpRequest();
    xmlhttp.onreadystatechange = function() {
      if (this.readyState == 4 && this.status == 200) {
        var myObj = JSON.parse(this.responseText);
        document.getElementById("data").innerHTML = myObj.response.visa.content;
      }
    };
    xmlhttp.open("GET", url, true);
    xmlhttp.send();
  </script>
  <script>
    function getURLParameter(name) {
      var value = decodeURIComponent((RegExp(name + '=' + '(.+?)(&|$)').exec(location.search) || [, ""])[1]);
      return (value !== 'null') ? value : false;
    }

    var param = getURLParameter('test');
    if (param) document.getElementById("testnr").innerHTML = param;

    var param1 = getURLParameter('test1');
    if (param1) document.getElementById("testnr1").innerHTML = param1;

    var url = "https://api01.otherdomain.com?test=" + param + "&test1" + param1 + "&trv=0&vis=1"
  </script>  
</body>

Any hint where the problem is with this code? 任何提示此代码出问题的地方?

Kind regards, Chris 亲切的问候,克里斯

It seems like you having issue with script execution order. 似乎您对脚本执行顺序有疑问。

I assume that testnr element coming from your XML ajax request. 我假设testnr元素来自您的XML ajax请求。

You have two script block in your HTML and it will executed while page load. HTML中有两个脚本块,它将在页面加载时执行。

When second script block is running your fist XMLHttpRequest not completed so it not able to find given HTML tag document.getElementById("testnr").innerHTML 当第二个脚本块正在运行时,您的第一拳XMLHttpRequest未完成,因此无法找到给定的HTML标记document.getElementById("testnr").innerHTML

To overcome this issue you need to execute script only after XMLHttpRequest request is completed. 要解决此问题,您仅需要在XMLHttpRequest请求完成后才执行脚本。

In your case : 在您的情况下:

var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
        var myObj = JSON.parse(this.responseText);
        document.getElementById("data").innerHTML = myObj.response.visa.content;
// Execute new created function here 
        SetValues();

    }
};
xmlhttp.open("GET", url, true);
xmlhttp.send();


function getURLParameter(name) {
  var value = decodeURIComponent((RegExp(name + '=' + '(.+?)(&|$)').exec(location.search) || [, ""])[1]);
  return (value !== 'null') ? value : false;
}

function SetValues()
{
var param = getURLParameter('test');
if (param) document.getElementById("testnr").innerHTML = param;

var param1 = getURLParameter('test1');
if (param1) document.getElementById("testnr1").innerHTML = param1;

var url = "https://api01.otherdomain.com?test=" + param + "&test1" + param1 + "&trv=0&vis=1"
}
</script>

Okay, i fixed the matter. 好吧,我解决了这个问题。

Here my result which works fine for me: 在这里,我的结果对我来说很好:

    <script>
function getURLParameter(name) {
  var value = decodeURIComponent((RegExp(name + '=' + '(.+?)(&|$)').exec(location.search) || [, ""])[1]);
  return (value !== 'null') ? value : false;
}

var param = getURLParameter('test');

var param1 = getURLParameter('test1');

var url = "https://api01.otherdomain.com?test=" + param + "&test1" + param1 + "&trv=0&vis=1"
</script>

<script>
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
        var myObj = JSON.parse(this.responseText);
        document.getElementById("additionalContent").innerHTML = myObj.response.additionalContent;
        document.getElementById("data").innerHTML = myObj.response.visa.content;
    }
};
xmlhttp.open("GET", url, true);
xmlhttp.send();

</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM