[Solved] Post request to external server returns nothing


#1

On the client, I want to run this:

$.post(Session.get("website") + "wp-admin/admin-ajax.php", toSubmit, function(error, response){
	console.log("Article ID", response);
});

When deploying and running in the browser, this works fine. When deploying and running as an Android app, toSubmit is submitted (and does the appropriate stuff on the external server), but a response is never received/processed in the app. And an error is never returned.

Meanwhile, the following works fine in every circumstance:

$.get(url, function(data, status, request) {
	if (data) {
		data._id = "" + data.id;
		News.upsert({_id: data._id}, data);
				
		FlowRouter.go("/article/" + data._id);
			
	}
}, 'json');

I’ve also tried post requests through the HTTP package, but those, too, fail when deployed to mobile.

How can this be resolved? That is, how can I do a POST request from the client to an external server and read the response?


#2

Seems like a CORS issue. Check about Cross-Origin Resource Sharing for mobile apps


#3

Thanks.

I already had this in the remote server’s .htaccess:

Header set Access-Control-Allow-Headers "Origin, X-Requested-With, Content-Type, Accept"

I added this to the page on the remote server that was returning results:

header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Headers: Cache-Control, Pragma, Origin, Authorization, Content-Type, X-Requested-With');
header('Access-Control-Allow-Methods: GET, PUT, POST');

The latter seems to have done the trick.