连接http服务器 "GET" 和 "POST" 成功代码

GET方法

        // var str="platName=default_win&sid=4535345"
        var ServerLink="http://192.168.1.134:8080/center_account/account/user/login.do?platName=default_win&sid=4535345";
        var xhr = new XMLHttpRequest();
        // var self = this
        xhr.onreadystatechange = function () {
            if (xhr.readyState == 4 && (xhr.status >= 200 && xhr.status <= 400)) {
                console.log("连接成功");
                var response = xhr.responseText;
                console.log(response);
            } else {
                cc.log("1111111111111");
                cc.log(xhr.readyState);
                cc.log(xhr.status);
                var response = xhr.responseText;
                console.log(response);
                cc.log("22222222222222222");

            }
        };
        xhr.open("GET", ServerLink, true);
        xhr.send();

POST方法

        var str="platName=default_win&sid=4535345"
        //var ServerLink="http://192.168.1.134:8080/center_account/account/user/login.do?";
        var ServerLink="http://a.donkey.game:9020/center_account/account/user/login.do?";
        
        var xhr = new XMLHttpRequest();
        // var self = this
        xhr.onreadystatechange = function () {
            if (xhr.readyState == 4 && (xhr.status >= 200 && xhr.status <= 400)) {
                console.log("连接成功");
                var response = xhr.responseText;
                console.log(response);
            } else {
                cc.log("1111111111111");
                cc.log(xhr.readyState);
                cc.log(xhr.status);
                var response = xhr.responseText;
                console.log(response);
                cc.log("22222222222222222");

            }
        };
        xhr.open("POST", ServerLink);
        xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=utf-8");
        xhr.send(str);

网页端报错


image.png

是服务器跨域问题
Access-Control-Allow-Origin

服务器还要加上


image.png

你可能感兴趣的:(连接http服务器 "GET" 和 "POST" 成功代码)