COCOS2D-JS cross-domain access issues, COCOS2D-JS request network data

Source: Internet
Author: User
Here is the code for COCOS2D-JS to request network data:

var HttpRequest = {/* * network request GET * URL Request Network address * Callback CALLBACK parameter * */get:function (url,callback) {        var xhr = Cc.loader.getXMLHttpRequest ();        Xhr.open ("GET", url,true); Xhr.onreadystatechange = function () {if (xhr.readystate = = 4 && (xhr.status >= && xhr.            Status <= 207) {err = false;            }else{err = true;            } var response = Xhr.responsetext;        Callback (Err,response);        };    Xhr.send ();    },/* * Network request post * URL Request network address * Params request parameter ("id=1&id=2&id=3") * Callback Callback parameter * */ Post:function (url,params,callback) {var nums = Arguments.length if (nums = = 2) {callback = Argumen            TS[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 >= && 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) {//error handling} else{cc.log (data);}}) */

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 be accessed across domains, and we find the file to be requested on the server: set the header

 
  

in this way, the data is returned successfully? But!!! , no success, Dizzy, because this PHP call header can not have any output, must be changed to encode UTF-8 no ROM format, get a night stuck at this point!!!

The above describes the Cocos2d-js cross-domain access issues, COCOS2D-JS request network data, including aspects of the content, I hope that the PHP tutorial interested in a friend helpful.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.