AJAX POST not working on iOS devices

1,990 views
Skip to first unread message

dad...@dadacomunica.com

unread,
Jun 20, 2014, 3:31:43 AM6/20/14
to mel...@googlegroups.com
Hi there!

I have a strange problem relating AJAX POST requests and iOS... I'm working in a MelonJS game that, at some points, should do a "POST" AJAX call to a server PHP script while sending a huge data string (that can't be send through "GET"); then, the server should return the URL of an image. The whole thing works fine while testing it on Android devices using CocoonJS, but doesn't work on iOS devices and CocoonJS: the data string is generated well, but the server receives nothing (as if the phone wasn't sending anything)... However, "GET" calls do send data without any problem!

As the same code works fine for Android, I suppose it should have something to do with some Apple policies or this kind of stuff, but I can't figure out what can be happening... I have some headers' properties defined in the server script, as I had some problems relating CORS some days ago:

if (isset($_SERVER['HTTP_ORIGIN'])) {
    header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}");
    header('Access-Control-Allow-Credentials: true');
    header('Access-Control-Max-Age: 86400');
}


I've asked around some forums and searched for some solutions, but I can't find anything useful for me... Any idea on what can be happening? Has someone found the same problem, maybe?

Thanks in advance for your time and effort! :)

Jay Oster

unread,
Jun 20, 2014, 12:58:31 PM6/20/14
to mel...@googlegroups.com, dad...@dadacomunica.com
If it's related to CORS, you may have to support preflight POST requests. This works by the client issuing an OPTIONS request to validate the POST method is allowed from the origin. You need to respond to the OPTIONS request with header:

Access-Control-Allow-Methods: POST, GET, OPTIONS

And then the client should send the POST request. More info on preflighted CORS requests: https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS#Preflighted_requests

dad...@dadacomunica.com

unread,
Jun 25, 2014, 5:59:28 AM6/25/14
to mel...@googlegroups.com, dad...@dadacomunica.com
Hi Jay!

I'm trying to get this working but I'm getting stucked with it... I have very little experience with CORS and requests, so I'm unable to understand where exactly I should place the code... I've tried adding the following to my PHP page:

header('Access-Control-Allow-Methods: POST, GET, OPTIONS');
header('Access-Control-Allow-Headers: *'); //X-PINGOTHER


(I've read that 'allow-headers' with '*' should be even more generic than 'X-PINGOTHER'), but it changes nothing... I've understood the whole thing refers to my PHP file (as the JS file already does what it should), but I'm really drowning with this... U_U'

However, adding these pair of "headers" to the PHP changes nothing, so I guess I'm missing something important...

Thanks for your time, and sorry for being that novice with this topic...! ;P


El divendres 20 de juny de 2014 18:58:31 UTC+2, Jay Oster va escriure:

Jay Oster

unread,
Jun 25, 2014, 3:25:24 PM6/25/14
to mel...@googlegroups.com, dad...@dadacomunica.com
You only need to allow arbitrary X-headers that you want to support. Your game will never send X-PINGOTHER (since it was just an example used in the article). And you probably won't ever send any arbitrary headers anyway, so don't worry about that.

Instead, try to determine if CocoonJS is attempting a preflighted-POST requests. Which means your script needs to handle the OPTIONS method as the article explains in detail.

A quick search turned up the following extra articles, which cover PHP specifically:
  1. http://www.dinochiesa.net/?p=754
  2. http://remysharp.com/2011/04/21/getting-cors-working/
You should use this info to help you debug the request on the server-side. Another thing you may want to do is packet capturing on the server with tcpdump. Just set the filters accordingly so you only capture HTTP traffic from the CocoonJS device. You can save the captures to the binary format and load them in Wireshark (a GUI) to make better sense of what's going on. Be warned though, this method of debugging is very low-level, and you just need to be aware of that.

dad...@dadacomunica.com

unread,
Jun 27, 2014, 3:57:11 AM6/27/14
to mel...@googlegroups.com, dad...@dadacomunica.com
Hi Jay!

Thanks, it took me some time to understand the whole thing, but now I think I've managed to get it working (still doing some tests, but it looks good)! ;)

El dimecres 25 de juny de 2014 21:25:24 UTC+2, Jay Oster va escriure:

Jay Oster

unread,
Jun 28, 2014, 12:10:59 AM6/28/14
to mel...@googlegroups.com, dad...@dadacomunica.com
That's awesome! Great news.
Reply all
Reply to author
Forward
0 new messages