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.