Cocos2d html5 js-binding XMLHttpRequest error

We have develop a game using cocos2d html5. Web version works very well but when i make js-binding to iPad there are some problems.

Does anyone knows how to solve ” ReferenceError: XMLHttpRequest is not defined ” error?
I got this error during cocos2d-html5 to cocos2d iphone js binding. I have to connect remote server to post/get some data. It works well on cocos2d-html5 but i could not bind it to native.

Looking forward to the answer , we have the same demand~
:stuck_out_tongue:

Hi, @hacioglu @jiwu_ma

Have you seen the XMLHttpRequest test case in our samples? It shows you have to do it correctly:

var xhr = cc.loader.getXMLHttpRequest();
xhr.open("GET", "http://httpbin.org/get");

xhr.onreadystatechange = function () {
	if (xhr.readyState == 4 && xhr.status == 200) {
		// Do stuff
	}
};
xhr.send();

You cannot use browser’s standard XMLHttpRequest directly, because there are no such thing in JSB, instead, you need to retrieve the a request object like this: var xhr = cc.loader.getXMLHttpRequest();.

Huabin

Hi @pandamicro,
I use cocos2d-iphone jsb bindings and get the same error.
If i try to use cc.loader.getXMLHttpRequest() i get a

cc.loader is undefined

Do i need to include something special like special bindings.js together with ccp-sources like in cocos2d-x?
Thank you.

Hi, @roelfsche

The API for Cocos2d-JS is now largely refactored, so the new API won’t work for cocos2d-iphone. cc.loader is one of these API changes which replaced cc.Loader.getInstance()
And sorry I don’t know whether cocos2d-iphone have proposed XMLHttpRequest

Huabin

Hi @pandamicro
Thats why i still use v2.x. So far i didn’t got it running with cocos2d-x.
If i try to use cocos2d-x v3.0 it isn’t possible to use the cc.BuilderReader because an error is thrown while trying to load a scene.

EXC_BAD_ACCESS here:
    JS_DefineFunction(cx, tmpObj, "create", js_CocosBuilder_create, 2, JSPROP_READONLY | JSPROP_PERMANENT);

I think the column before

    JSObject  *tmpObj = JSVAL_TO_OBJECT(anonEvaluate(cx, obj, "(function () { return cc._Reader; })()"));

is responsible for that because it returns NULL for tmpObj.
I already searched the project for the string cc._Reader but nothing is found. Isn’t it supported anymore?

I got it running.
you need to include

#include "cocosbuilder/js_bindings_ccbreader.h"
#include "jsb_cocos2dx_builder_auto.hpp"

in AppDelegate.cpp and then call

sc->addRegisterCallback(register_all_cocos2dx_builder);
sc->addRegisterCallback(register_CCBuilderReader);

Now the cc.BuilderReader.load works for me.