码迷,mamicode.com
首页 > Windows程序 > 详细

JS Call API

时间:2016-02-27 16:30:15      阅读:199      评论:0      收藏:0      [点我收藏+]

标签:

    <script>

        function createXMLHttpRequest() {
            var xmlHttp;
            if (window.ActiveXObject) {
                xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
            else if (window.XMLHttpRequest) {
                xmlHttp = new XMLHttpRequest();
            }
            xmlHttp.open("get", "https://merchant-api.jet.com/api/merchant-skus/00-980-001", true);
            xmlHttp.setRequestHeader("Content-Type", "application/json");
            xmlHttp.setRequestHeader("Authorization", "8YrpzzeM56dlCKAlFfdkNMsyGxPgkX844ZxjmASEogWgUMx0bVgAwDDeEjzZUE2W2b9E65DQo");
            xmlHttp.send(null);
            xmlHttp.onreadystatechange = function () {
                var checkResult = document.getElementById("checkResult");
                if (xmlHttp.readyState = 4) {
                    if (xmlHttp.status = 200) {
                        checkResult.innerHTML = xmlHttp.responseText;
                    }
                    else {
                        alert(xmlHttp.statusText);
                    }
                }
            };

        }

        function createXMLHttpRequestForPost() {
            var xmlHttpReq;
            xmlHttpReq = new XMLHttpRequest();
            xmlHttpReq.open("post", "https://merchant-api.jet.com/api/token/", true);
            xmlHttpReq.setRequestHeader("Content-Type", "application/json");
            xmlHttpReq.setRequestHeader("Authorization", "8YrpzzeM56dlCKAlFfdkNMsyGxPgkX844ZxjmASEogWgUMx0bVgAwDDeEjzZUE2W2b9E65DQo");
            var content = ‘{"user":"xxxx","pass":"xxxx"}‘;
            xmlHttpReq.send(content);
            xmlHttpReq.onreadystatechange = function () {
                var checkResult = document.getElementById("checkResult");
                if (xmlHttpReq.readyState = 4) {
                    if (xmlHttpReq.status = 200) {
                        checkResult.innerHTML = xmlHttpReq.responseText;
                    }
                    else {
                        alert(xmlHttpReq.statusText);
                    }
                }
            };
        }


    </script>
    <p id="checkResult">aaa</p>
    <button onclick="createXMLHttpRequest()">Call API</button>
    <button onclick="createXMLHttpRequestForPost()">Call POST API</button>

 

JS Call API

标签:

原文地址:http://www.cnblogs.com/leon-y-liu/p/5222972.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!