XMLHttpRequest, WebSocket, CCHttpClient compatibility with iOS & Android

Hello,

I’ve been trying to get/post some data from my game to a webserver without success. I’m using cocos2d-x js to write code once for HTML, iOS, and Android versions of my game.
I’ve tried XMLHttpRequest on my HTML version it gave me the error “Origin is not allowed by Access-Control-Allow-Origin” which is a common error for javascript trying to get/post to a webserver. So I tried testing it on my iOS device i got the following error “XMLHttpRequest is not defined”.
If I try websocket i’ll also have “WebSocket is not defined” on iOS

Is there any alternative functions I could use that are compatible with the three platforms ?

Thanks,
Bassam

Hello.
I just wanted to write that if you finally find out how to do it, post it please :slight_smile:

Bass   wrote:

Hello,
>
I’ve been trying to get/post some data from my game to a webserver without success. I’m using cocos2d-x js to write code once for HTML, iOS, and Android versions of my game.
I’ve tried XMLHttpRequest on my HTML version it gave me the error “Origin is not allowed by Access-Control-Allow-Origin” which is a common error for javascript trying to get/post to a webserver. So I tried testing it on my iOS device i got the following error “XMLHttpRequest is not defined”.
If I try websocket i’ll also have “WebSocket is not defined” on iOS
>
Is there any alternative functions I could use that are compatible with the three platforms ?
>
Thanks,
Bassam

What’s your mean?

JS can use XmlHttpRequest as well like HTML version,
ensure your cocos2dx version has XmlHttpRequest.cpp/.h
please prefer my attachment.

Simon Pan wrote:

Bass   wrote:
> Hello,
>
> I’ve been trying to get/post some data from my game to a webserver without success. I’m using cocos2d-x js to write code once for HTML, iOS, and Android versions of my game.
> I’ve tried XMLHttpRequest on my HTML version it gave me the error “Origin is not allowed by Access-Control-Allow-Origin” which is a common error for javascript trying to get/post to a webserver. So I tried testing it on my iOS device i got the following error “XMLHttpRequest is not defined”.
> If I try websocket i’ll also have “WebSocket is not defined” on iOS
>
> Is there any alternative functions I could use that are compatible with the three platforms ?
>
> Thanks,
> Bassam
>
What’s your mean?
>
JS can use XmlHttpRequest as well like HTML version,
ensure your cocos2dx version has XmlHttpRequest.cpp/.h
please prefer my attachment.

Thanks for your quick reply guys.
I’ve checked again I have both XmlHttpRequest.cpp/.h in my project, but I have the XmlHttpHelper.h in red does that render it unusable ?
Refer to the screenshot i’ve attached

ME2,
but that’s not a big deal,
It can compiles as well.

I’m using cocos2d-x 2.1.4 if that makes any difference. I used project-creator to create my initial project. What did you use Simon ?

What version of cocos2d-x did you use and how did you create your project?

XmlHttpRequest.h supported after version 2.1.4.
I used create_project.py to create cross platform project like you.

I’ve just tried XMLHttpRequest in version 2.2.0 i’m still getting the same error : “XMLHttpRequest is not defined”
I’ve attached the output here

    cocos2d.x.version: 2.2.0
    cocos2d.x.compiled_with_profiler: false
    cocos2d.x.compiled_with_gl_state_cache: true
    gl.vendor: Apple Computer, Inc.
    gl.renderer: Apple Software Renderer
    gl.version: OpenGL ES 2.0 APPLE-9.1.13
    gl.max_texture_size: 4096
    gl.max_texture_units: 8
    gl.max_samples_allowed: 4
    gl.supports_PVRTC: true
    gl.supports_NPOT: true
    gl.supports_BGRA8888: false
    gl.supports_discard_framebuffer: true
    gl.supports_vertex_array_object: true

Cocos2d: Get data from file(main.jsc) failed!
Cocos2d: Get data from file(jsb.jsc) failed!
Cocos2d: Get data from file(jsb_cocos2d_constants.jsc) failed!
Cocos2d: Get data from file(jsb_cocos2d.jsc) failed!
Cocos2d: Get data from file(jsb_cocos2d_extension.jsc) failed!
Cocos2d: Get data from file(jsb_chipmunk_constants.jsc) failed!
Cocos2d: Get data from file(jsb_chipmunk.jsc) failed!
Cocos2d: Get data from file(jsb_opengl_constants.jsc) failed!
Cocos2d: Get data from file(jsb_opengl.jsc) failed!
Cocos2d: Get data from file(jsb_cocosbuilder.jsc) failed!
Cocos2d: Get data from file(jsb_sys.jsc) failed!
Cocos2d: Get data from file(jsb_deprecated.jsc) failed!
Cocos2d: Get data from file(MainScene.jsc) failed!
Cocos2d: JS: /Users/bass/Library/Application Support/iPhone Simulator/7.0-64/Applications/ED9BD3B3-F1A4-4252-BF5D-94DE4B9FA426/MyGame.app/Published-iOS/MainScene.js:5:ReferenceError: XMLHttpRequest is not defined

Tobias S wrote:

Hello.
I just wanted to write that if you finally find out how to do it, post it please :slight_smile:

Hey everyone,

I’ve solved the issue and I’m now able to use XmlHttpRequest on cocos2d-x 2.1.4.
I had the following line missing in my “Classes/AppDelegate.cpp”

sc->addRegisterCallback(MinXmlHttpRequest::_js_register);

Thanks for your help !