Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/441.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在另一个httprequest中使用来自一个httprequest的JSON值_Javascript_Jquery_Html_Xmlhttprequest - Fatal编程技术网

Javascript 在另一个httprequest中使用来自一个httprequest的JSON值

Javascript 在另一个httprequest中使用来自一个httprequest的JSON值,javascript,jquery,html,xmlhttprequest,Javascript,Jquery,Html,Xmlhttprequest,我正在尝试将开放数据与2个http请求结合起来。我需要使用第一个响应的一个值来生成第二个请求url(该值是第二个url的一部分)。我该怎么做 这是我的密码: XMLHttpRequest zipcode 小时信噪比 得到满足 响应1 响应2 函数loadDoc(){ var xhttp=newXMLHttpRequest(); xhttp.onreadystatechange=函数(){ if(this.readyState==4&&this.status==200){ var myObj=

我正在尝试将开放数据与2个http请求结合起来。我需要使用第一个响应的一个值来生成第二个请求url(该值是第二个url的一部分)。我该怎么做

这是我的密码:


XMLHttpRequest
zipcode

小时信噪比

得到满足 响应1

响应2

函数loadDoc(){ var xhttp=newXMLHttpRequest(); xhttp.onreadystatechange=函数(){ if(this.readyState==4&&this.status==200){ var myObj=JSON.parse(this.responseText); document.getElementById(“demo”).innerHTML=myObj.response.docs[0].id; //myObj.response.docs[0].id是下一个httprequest需要的值/变量 } }; xhttp.open(“GET”https://geodata.nationaalgeoregister.nl/locatieserver/v3/suggest?q=“+document.getElementById(“zip”).value++”+document.getElementById(“housenr”).value+”&wt=json”,true); xhttp.send(); }; 函数loadDoc2(){ 变量url=”https://geodata.nationaalgeoregister.nl/locatieserver/v3/lookup?id=adr-16dc4e7caee6f2b34222fb02b91a464e“//值/变量myObj.response.docs[0]。id应该是url的最后一部分(从“adr.”到“64e”) var vhttp=新的XMLHttpRequest(); vhttp.onreadystatechange=函数(){ if(this.readyState==4&&this.status==200){ var Obj=JSON.parse(this.responseText); document.getElementById(“demo2”).innerHTML=this.responseText; } }; vhttp.open(“GET”,url,true); vhttp.send(); };
您必须将id作为以下内容传递给loadDoc2:

function loadDoc2(id) {        
var url = "https://geodata.nationaalgeoregister.nl/locatieserver/v3/lookup?id="+id;//the value/variable myObj.response.docs[0].id should be the last part of the url (from "adr." to "64e")
var vhttp = new XMLHttpRequest();
vhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
var Obj = JSON.parse(this.responseText);
document.getElementById("demo2").innerHTML = this.responseText;

 }
 }; 
  vhttp.open("GET",url, true); 
  vhttp.send();
};
并在loadDoc成功时调用它:

function loadDoc() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
  if (this.readyState == 4 && this.status == 200) {
  var myObj = JSON.parse(this.responseText);
  document.getElementById("demo").innerHTML = myObj.response.docs[0].id;
  loadDoc2(myObj.response.docs[0].id)
  //myObj.response.docs[0].id is the value/variable I need for the next httprequest
}
 };

  xhttp.open("GET", "https://geodata.nationaalgeoregister.nl/locatieserver/v3/suggest?q=" + document.getElementById("zip").value +"+"+ document.getElementById("housenr").value +"&wt=json", true);
xhttp.send();
  };

您只需使用id参数调用
loadDoc2()

function loadDoc() {
  var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
    var myObj = JSON.parse(this.responseText);
    var response_id = myObj.response.docs[0].id;
    document.getElementById("demo").innerHTML = response_id;
    //myObj.response.docs[0].id is the value/variable I need for the next httprequest
    loadDoc2(response_id);
  }
};
  xhttp.open("GET", "https://geodata.nationaalgeoregister.nl/locatieserver/v3/suggest?q=" + document.getElementById("zip").value +"+"+ document.getElementById("housenr").value +"&wt=json", true);
  xhttp.send();
 };

  function loadDoc2(response_id = '') {        
   var url = "https://geodata.nationaalgeoregister.nl/locatieserver/v3/lookup?id="+response_id;
   var vhttp = new XMLHttpRequest();
   vhttp.onreadystatechange = function() {
   if (this.readyState == 4 && this.status == 200) {
   var Obj = JSON.parse(this.responseText);
   document.getElementById("demo2").innerHTML = this.responseText;
  }
}; 
  vhttp.open("GET",url, true); 
  vhttp.send();
};
onreadystatechange
处理程序中,您可以将id提取到
request\u id
中,并使用它调用
loadDoc2(request\u id)
。此外,不要忘记将onclick处理程序更改为仅调用loadDoc():

获取内容

您的请求可能同时运行@photo1的副本。调用
loadDoc2
时,第一个请求仍在运行。您应该在第一个请求的
onreadystatechange
内调用
loadDoc2
。@elias,我不知道这个线程。在我的搜索结果中没有出现
function loadDoc() {
  var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
    var myObj = JSON.parse(this.responseText);
    var response_id = myObj.response.docs[0].id;
    document.getElementById("demo").innerHTML = response_id;
    //myObj.response.docs[0].id is the value/variable I need for the next httprequest
    loadDoc2(response_id);
  }
};
  xhttp.open("GET", "https://geodata.nationaalgeoregister.nl/locatieserver/v3/suggest?q=" + document.getElementById("zip").value +"+"+ document.getElementById("housenr").value +"&wt=json", true);
  xhttp.send();
 };

  function loadDoc2(response_id = '') {        
   var url = "https://geodata.nationaalgeoregister.nl/locatieserver/v3/lookup?id="+response_id;
   var vhttp = new XMLHttpRequest();
   vhttp.onreadystatechange = function() {
   if (this.readyState == 4 && this.status == 200) {
   var Obj = JSON.parse(this.responseText);
   document.getElementById("demo2").innerHTML = this.responseText;
  }
}; 
  vhttp.open("GET",url, true); 
  vhttp.send();
};
<button type="button" onclick="loadDoc();" >Get Content</button>