NetworkError: Failed to execute 'send' on XMLHttpRequest': Failed to load - android

Server side is Django/python.
Client environment is cordova/android.
This request fails :
$.ajax({
url: myAddress,
type: 'POST',
data: mydata,
crossDomain: true,
processData: false,
contentType: false,
async: false,
success: function () { mycode},
error: function (xhr) { mycode} }
});
When myAddress is 192.168.1.11 it works perfectly.
When myAddress is dev.mysite.com i get the error:
NetworkError: Failed to execute 'send' on XMLHttpRequest': Failed to load dev.mysite.com/../..
However, i can connect to the same django web server with chrome on the mobile device.
config.xml contains : access origin="*"
Do you have an idea to solve this problem?

The js code is good.
The CSP code is good.
My problem came from my misunderstanding of http redirection at the DNS level.
Thank you for you attention

Related

How to resolve ssl issue in ionic3 filetransfer?

I'm facing an issue while trying to use the IONIC Cordova File Transfer plugin to upload a file while the laravel Server is running in HTTPS.
I'm able to login to the application and access all APIs from the server with get and post, but when I try to use fileTransfer for uploading data I'm getting the below message.
body: null
code: 3
exception: "java.security.cert.CertPathValidatorException: Trust anchor for certification path not found."
http_status: null
source: "content://com.android.providers.media.documents/document/image%3A16679"
target: "https://reddyflix.com/api/profile/photo/update"
I have dowloaded the (.cer) certificate file into the www directory of the project and setSSLCertMode = pinned. But I am still facing this issue.
let options: FileUploadOptions = {
httpMethod: "post",
chunkedMode: false,
fileKey: 'file',
fileName: 'name.png',
headers: headers
};
let trustAllHosts = true;
fileTransfer.upload(
uri,encodeURI('https://domain/api/profile/photo/update'), options,trustAllHosts)
.then((data) => {
console.log(data);
}, (err) => {
console.log(err);
})

React-native POST request in android over https return network error

Tried to create user account through https POST request under react-native with axios, while it always failed on android with a 'network error'.
axios('https://'+DEST_URI, {
method: 'post',
data: account,
headers: {
'Accept': 'application/json',
}
then(...)
The same https POST request works fine on iOS.
Changed to http, the same POST request works on android as well.
Also tried GET request through https on android, it could retrieve data from the backend server as expected.
Any idea about it?
Linked image is the output from console log,
output from console log
I was facing the same issue with android 'POST' requests. Turned out to be headers issue as mentioned in the link: https://github.com/facebook/react-native/issues/5222#issuecomment-170239302
Adding the following headers fixed the issue for me
headers = {
'Accept': 'application/json',
'Authorization': 'Bearer ' + this.authToken,
'Content-Type': 'application/x-www-form-urlencoded',
}
Related to the issue Upgrade to OkHttp3 on the react-native repository.
Deactivate http2 from your proxy that is what causing the problem.
You can try to upgrade to react-native v0.27 they mention in the change log that they fixed this problem but for me didn't work.
I had to disable the http2 feature in ngnix to make it work.
disabling http2 really is this simple, just change this line in your ngnix config file from:
listen 443 ssl http2;
to
listen 443 ssl;
then reload your config:
service nginx reload
More info about http2 and ngnix can be found here
I guess you have not a internet permission in your AndroidManifest.xml file.
Please add this line into you AndroidManifest.xml file:
<uses-permission android:name="android.permission.INTERNET" />

Cordova android app - POST request returns "forbidden access" error

For some reason my Cordova built app cannot send POST requests as a mobile application. If I run it from a browser (in my PC or mobile device) it works fine, but when I run it as mobile app request fails giving 403 forbidden error. Maybe someone has encountered similar problem before and knows what to do?
P.S. GET requests work fine.
config.xml:
<access origin="mytestserver.eu/test"/>
.js:
$.ajax({
type: 'POST',
url: "http://mytestserver.eu/test",
data: '{ "test": "Test"}',
dataType:'json',
headers: {
'Content-Type': 'application/json'
},
crossDomain: true,
success: function(data, textStatus, request){
alert ($.toJSON(data));
},
error: function (request, textStatus, errorThrown) {
alert ($.toJSON(errorThrown));
}
});
This issue was resolved when I disabled ModSecurity on my server.
For me, I was able to do that via the cPanel access to my host:
cPanel > Security > ModSecurity

Phonegap Cross-domain AJAX POST Request not working on Android

Cross-domain AJAX POST request works perfectly fine on web browsers including browsers on mobile phones, but doesn't work for native applications built using Phonegap
I have created a login form that users have to enter their login credentials, then they are verified by the server that is hosted on heroku and returns json {"success":true} if valid credentials are entered.
My Ajax script:
$.ajax({
type: "POST",
url: "http://domain.com/public/auth/app-login",
contentType: "application/x-www-form-urlencoded; charset=utf-8",
dataType: "json",
data: {identity: <username from form>, password: <password from form>},
crossDomain: true,
cache: false,
success: function(data) {
obj = JSON.parse(data);
if (obj && obj.success === true) {
window.location.href = 'home.html';
}
},
error: function(e) {
alert('Error: ' + e.message);
}
});
Steps taken to resolve this issue:
Domain whitelisting - config.xml
<access origin="http://domain.com/public/auth/app-login" />
<access origin="*" />
Telling jQuery to allow cross-domain
$.support.cors = true;
OR
jQuery.support.cors = true;
Disable caching cache: false
Any help is appreciated.
Ok. If index.html in local then you can call ajax any hosts, not need enable CORS in client or server. You remove:
$.support.cors = true; OR jQuery.support.cors = true;
And:
<access origin="http://domain.com/public/auth/app-login" />
It redundant, only use:
<access origin="*" />
You need check and add in AndroidManifest.xml:
<uses-permission android:name="android.permission.INTERNET" />
Add more permission if your app required. Finally, call ajax inside $(document).ready():
$.ajax({
type: "POST",
url: "http://domain.com/public/auth/app-login",
dataType: "json",
data: {identity: <username from form>, password: <password from form>},
success: function(data) {
obj = JSON.parse(data);
if (obj && obj.success === true) {
window.location.href = 'home.html';
}
},
error: function(e) {
alert('Error: ' + e.message);
}
});
If you are looking to resolve this issue then you may wish to make sure that the plugin is being properly included into the build process.
RESOURCE: \app\config.xml
<widget>
.... [lots of stuff] ....
<gap:plugin name="com.indigoway.cordova.whitelist.whitelistplugin" />
<access origin="http://*" />
....
</widget>
You may also wish to specify a version as that is recommended, and I do not specify one above. A good way to test if the plugin is included is to use the free cloud account provided at, https://build.phonegap.com/apps. If you build your project there you can check the plugins tab and make sure that the whitelist plugin is included.
I have read that you should only need this in the HEAD element of your HTML page, but I found as of the date of this post that I still needed to include the plugin.
<meta http-equiv="Content-Security-Policy" content="default-src *; style-src 'self' 'unsafe-inline'; script-src 'self' 'unsafe-inline' 'unsafe-eval'">
If the plugin is not loaded you might get a "Not Found" error when using the $.ajax method for jQuery for the error string.
Some of the information on the Internet will tell you that the whitelist information is placed into the /www/res/ folder, but this appears to be outdated information. You may also find that <plugin... is used in some examples, but it appears that this may be an obsolete way?
Also, you may need:
RESOURCE: \app\config.xml
<widget>
...
<feature name="http://api.phonegap.com/1.0/network"/>
...
</widget>
use
JSON.stringify(data: {identity: <username from form>, password: <password from form>})
instead of
data: {identity: <username from form>, password: <password from form>}
I got success message when i changed my code like this.
Some time there is issue in your domain. In my case I resolved it by putting following code in my .htaccess file
<IfModule mod_headers.c>
Header set Access-Control-Allow-Origin "*"
</IfModule>

Ajax cache: false in PhoneGap 2.1 loading error

I found that the ajax call loading problem occurs when cache: false is added. I have tested it on three mobile phones: Samsung Galaxy SIII (Android 4.1.2), Sony Xperia P (Android 4.0.4) and LG (Android 4.0.3). Only SIII has no problem. Sony and LG phones just keep loading when calling ajax. How to solve it?
This following code is the ajax call mentioned:
$.ajax({
url: serviceURL ,
async: true,
cache: false,
type: 'POST',
dataType: 'json',
contentType: 'application/json',
data: JSON.stringify({
.......
}),
success: function(data) {
......
}
});
If I removed cache: false, the three phones run the ajax normally.
The mobile app uses PhoneGap 2.1, Backbone.js, Jquerymobile 1.2 and Jquery 1.8.2.
Pages fetched with POST are never cached, so the cache and ifModified options in jQuery.ajaxSetup() have no effect on these requests.

Categories

Resources