2016-07-16 69 views
-1

試圖讓我的代碼連接到API並檢索本地農民市場的列表,但不斷得到參考錯誤xmlhttp沒有定義在第40行。沒有發現任何拼寫錯誤,並有嘗試移動大量代碼以查看它們是否在不同的位置工作。參考錯誤找不到xmlhttp

<html> 

    <head> 
     <meta name="viewport" content="width=device-width, initial-scale=1.0"> 
     <meta charset="UTF-8"> 

     <title>Markets</title> 
     <script> 
      window.onload = function() { 

       function myFunction(arr) { 
        out = "<h1>Markets</h1>"; 
        var i; 
        for (i = 0; i < array.results.length; i++) { 
         out = out + "<em>" + item.marketname + "</em><br>" + details.marketdetails.Address + "</p>" 
         arr.results.forEach(printDetails) 

        } 
        document.getElementById("market_details").innerHTML = out; 
       } 

       var mybutton = document.getElementById("submit_btn") 
       mybutton.onclick = function() { 

        var xmlhttp = new XMLHttpRequest(); 
        var zip = document.getElementById("zip").value 
        var url = "http://search.ams.usda.gov/farmersmarkets/v1/data.svc/zipSearch?zip=" + zip; 

        xmlhttp.onreadystatechange = function() { 
         if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { 
          var myArr = JSON.parse(xmlhttp.responseText); 
          myFunction(myArr); 
         } 
        } 
       }; 

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

      itemsProcessed = 0 

      function printDetails(item, index, array) { 

       console.log(item.marketname) 

       var xmlhttp = new XMLHttpRequest(); 
       var url2 = "http://search.ams.usda.gov/farmersmarkets/v1/data.svc/mktDetail?id=" + id; 

       xmlhttp.onreadystatechange = function() { 
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { 
         var myDArr = JSON.parse(xmlhttp.responseText); 
         details = myDArr 

         //console.log(myDArr.marketdetails) 

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

        itemsProcessed++ 
       } 
      } 

     </script> 
    </head> 

    <body> 
     <h1> Find Your Local Market!<h1> 
    Enter Zip Code:<input id="zip"></p><br> 
    <button id = "submit_btn">Submit</button> 
    <div id="market_details"></div> 
    </body> 
+0

請說明您知道/認爲問題出在哪一行。如果你說「第40行」,可能沒有人會花時間來計算第40行:) –

回答

1

第一個問題是你定義的onclick處理器內部xmlhttp和你所使用的處理程序外:

mybutton.onclick = function() { 
    var xmlhttp = new XMLHttpRequest(); //<-- This must be used from inside the current function 
    //Your code here 
    xmlhttp.open("GET", url, true); //<-- put this inside the function 
    xmlhttp.send(); //<-- put this inside the function 
} 

的第二個問題是,你opensendXMLHttpRequestonreadystatechange處理程序中:

xmlhttp.onreadystatechange = function() { 
    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { 
     var myDArr = JSON.parse(xmlhttp.responseText); 
     details = myDArr 

     //console.log(myDArr.marketdetails) 

    }; 

    itemsProcessed++ 
} 
xmlhttp.open("GET", url2, true);//<-- put this outside the onreadystatechange handler 
xmlhttp.send();//<-- put this outside the onreadystatechange handler 

我希望這會幫助你。

+0

謝謝。我提出了你的建議,並且我不再收到錯誤消息。 – Jane123