I'm use Cordova 5.2.0 to create a simple mobile application for Android and I try send a POST request to the server with Apigility 1.3.1 along with an additional module called 'ZfrCors' (ZfrCors is a simple Zend Framework 2 module that helps you to deal with Cross-Origin Resource Sharing), but the server responded with a status of 500 (Internal Server Error). Probably the fault causes header post like "Origin: file://"
I browse logs with Wireshark and see:
POST /mobileapp/user HTTP/1.1
Host: api.XXX.com
Connection: keep-alive
Content-Length: 15
Accept: */*
Origin: file://
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.89 Safari/537.36
Content-Type: application/json
Accept-Encoding: gzip, deflate
Accept-Language: en-US;q=0.6,en;q=0.4
{"name":"test"}
Here's my JavaScript Code:
var data = {
name: 'test'
};
$.ajax({
method: 'POST',
url: 'http://api.XXX.com/mobileapp/user',
data: JSON.stringify(data),
contentType: 'application/json',
error: function(jqXHR, textStatus, errorThrown)
{
console.log(jqXHR);
console.log(textStatus);
console.log(errorThrown);
alert('error');
},
success: function(data, textStatus, jqXHR)
{
alert('success');
}
});
The Config.xml file (Cordova configurations) contains
<access origin="http://api.XXX.com/mobileapp/user" />
In addition, the HEAD of index.html file includes
<meta http-equiv="Content-Security-Policy" content="default-src *; style-src 'self' 'unsafe-inline'; script-src 'self' 'unsafe-inline' 'unsafe-eval'">
On the server side ZF2 module 'ZfrCors' seems to be configured correctly, because when I create a POST request, for example, by the 'HttpRequester'-add-on for Firefox, without header "Origin: file://" everything works fine
zfr_cors.global.php
return array(
'zfr_cors' => array(
'allowed_origins' => array('*'),
'allowed_methods' => array('GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'),
'allowed_headers' => array('*'),
// 'max_age' => 120,
// 'exposed_headers' => array(),
//'allowed_credentials' => true,
));
It seems to me that the problem is Cordova, Webview sending incorrect Origin header?