Home > Backend Development > PHP Tutorial > cocos2d-js cross-domain access problem, cocos2d-js requests network data

cocos2d-js cross-domain access problem, cocos2d-js requests network data

WBOY
Release: 2016-07-29 09:13:10
Original
1481 people have browsed it

The following is the code for cocos2d-js to request network data:

var HttpRequest = {
    /*
     * 网络请求之GET
     * url 请求的网络地址
     * callback 回调参数
     * */
    GET:function(url,callback){
        var xhr = cc.loader.getXMLHttpRequest();
        xhr.open("GET",url,true);
        xhr.onreadystatechange = function () {
            if (xhr.readyState == 4 && (xhr.status >= 200 && xhr.status <= 207)) {
                err = false;
            }else{
                err = true;
            }
            var response = xhr.responseText;
            callback(err,response);
        };
        xhr.send();
    },
    /*
     * 网络请求之POST
     * url 请求的网络地址
     * params  请求参数  ("id=1&id=2&id=3")
     * callback 回调参数
     * */
    POST:function(url,params,callback){
        var nums = arguments.length
        if(nums == 2){
            callback = arguments[1];
            params = "";
        }
        var xhr = cc.loader.getXMLHttpRequest();
        xhr.open("POST", url);
        xhr.setRequestHeader("Content-Type","text/plain;charset=UTF-8");
        xhr.onreadystatechange = function () {
            if (xhr.readyState == 4 && (xhr.status >= 200 && xhr.status <= 207)) {
                err = false;
            }else{
                err = true;
            }
            var response = xhr.responseText;
            callback(err,response);
        };
        xhr.send(params);
    }
}

//EXMAPLE
/*
 HttpRequest.POST("http://127.0.0.1:3000/test","id=1&ids=2", function(err,data){
 if(err){
 //错误处理
 }else{
 cc.log(data);
 }
 })
 */
Copy after login

However, the request network appears: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost: 63342' is therefore not allowed access.

This is because the browser cannot access cross-domain. We find the file to be requested on the server side: set the header

<?php 
header("Access-Control-Allow-Origin : *");
echo "I Love you"
?>
Copy after login

In this way, the data is returned successfully? but! ! ! , failed, dizzy, the reason is that this php cannot have any output before calling the header, the encoding must be changed to UTF-8 without rom format, I got stuck at this point all night! ! !

The above introduces the cross-domain access issue of cocos2d-js. Cocos2d-js requests network data, including aspects of it. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template