引用错误找不到xmlhttp

时间:2016-07-16 22:15:49

标签: javascript api

尝试让我的代码连接到API并检索当地农贸市场列表,但不断获得xmlhttp未在第40行定义的参考错误。没有发现任何拼写错误并尝试移动块代码,看他们是否在不同的位置工作。

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

1 个答案:

答案 0 :(得分:1)

第一个问题是你在xmlhttp处理程序中定义onclick并在处理程序之外使用它:

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
}

第二个问题是你open处理程序中的sendXMLHttpRequest {/ 1}}:

onreadystatechange

我希望这会对你有所帮助。