js跨域调http接口(jsonp)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
<input type="text" id="searchInput"/>
<script>
    var success_jsonpCallback = function (result) {
        console.log("--------------------", typeof result, result);
    };
    document.querySelector("#searchInput").oninput = function (e) {
        if (document.querySelector("#searchPersonJsonp")) {
            this.parentNode.removeChild(document.querySelector("#searchPersonJsonp"));
        }
        var JSONP = document.createElement("script");
        JSONP.id = "searchPersonJsonp";
        JSONP.type = "text/javascript";
        JSONP.src = "http://21.57.14.2/searchperson.ashx?value=" + this.value + "&jsoncallback=success_jsonpCallback";
        this.parentNode.appendChild(JSONP);
    };
</script>
</body>
</html>

相关推荐