无法使用FOR循环

时间:2017-07-04 10:59:00

标签: javascript jquery html rest cors

我试图通过" onClick"的触发器从机器X向机器Y发送JSONP(CORS)请求。点击一些HTML按钮时。 但似乎它没有达到FOR循环,也没有发送请求。

另外,我想将每个域的响应呈现为与HTML分开。 如果您有任何提示,我也很高兴听到它。

var axios = require('axios');
require('promise/polyfill-done');
var jsonp = require('jsonp');

var xhttp = new XMLHttpRequest();
var hosts2 = ['http://host1.domain.net:8082/taskStatus','http://host2.domain.net:8082/taskStatus','http://host3.domain.net:8082/taskStatus','http://host4.domain.net:8082/taskStatus'];
var ledTypes2 = {
    green: "<img id='logo' src='green.png' height='30' width='30'>",
    red: "<img id='logo' src='red.png' height='30' width='30'>",
    yellow: "<img id='logo' src='yellow.png' height='30' width='30'>"
};

var lengthVal = hosts2.length;

function collectionChecking() {
    var mycallback = 'c'+Math.floor((Math.random()*100000000)+1);
    for (var hostIndx2 = 0; hostIndx2 < lengthVal; hostIndx2++) {
            console.log(hostIndx2);
            $.ajax({
                type: 'GET',
                url: hosts2[hostIndx2],
                async: true,
                cache: true,
                crossDomain: true,
                jsonpCallback: mycallback,
                timeout: 30000,
                contentType: "application/json",
                dataType: 'jsonp'

            }).done(function (json) {
                console.log(json);
                callback(json);
            }).fail(function (f) {
                console.log("f");
                console.log(f.status);
            }).always(function () {
                // console.log('complete');
            });
        }
    }

的index.html:

<!DOCTYPE html>
<html>
<head>
    <link rel="stylesheet" href="//code.jquery.com/ui/1.11.2/themes/smoothness/jquery-ui.css">
    <script src="//code.jquery.com/jquery-1.10.2.js"></script>
    <script src="//code.jquery.com/ui/1.11.2/jquery-ui.js"></script>
    <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
    <script src="http://code.jquery.com/jquery-2.0.0.min.js"></script>
    <script type="text/javascript" src="kongReq.js"></script>
    <script type="text/javascript" src="collectionReq.js"></script>
</head>

<body>
<h1><img src="logo.png"></h1>


<!-- JQuery tabs -->
<div id="tabs">
  <ul>
    <li><a href="#tabs-1">Integrations</a></li>
    <li><a href="#tabs-2" onclick="kongChecking(); collectionChecking();">Services status</a></li>
    <li><a href="#tabs-3">Documetations</a></li>
</ul>

</body>
</html>

0 个答案:

没有答案