2

I am trying to do a simple ajax POST from domain1 to domain2 using Axios. This is a cross domain simple POST so there is no PREFLIGHT (OPTIONS) call. The response from the application is a simple JSON string.

On Chrome, on Android, Windows and iOS (excluding iPhone) this works fine. But on iPhone 6,7,8+ on both Safari and Chrome i get an error in the console from the axios response.I can see the POST request get to the application on domain2 and a json response is sent. But this is what is shown when i console.log the response in the axios.catch. There are no other details.

Error: Network Error

My POST is a multipart/form-data post with the following Request headers:

Accept: application/json, text/plain, */*
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary81kouhSK7WgyVQZ3
Origin: https://domain1
Referer: https://domain1/test
User-Agent: Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1

And the form data is simply 4 text fields

------WebKitFormBoundary81kouhSK7WgyVQZ3
Content-Disposition: form-data; name="a"
12345
------WebKitFormBoundary81kouhSK7WgyVQZ3
Content-Disposition: form-data; name="b"
asdfasf
------WebKitFormBoundary81kouhSK7WgyVQZ3
Content-Disposition: form-data; name="c"
asdfadsf
------WebKitFormBoundary81kouhSK7WgyVQZ3
Content-Disposition: form-data; name="d"
adfasdfa
------WebKitFormBoundary81kouhSK7WgyVQZ3--

When the POST is sent from Chrome, (or IE and Firefox) on Windows and Mac I get the following response headers and a HTTP 200:

access-control-allow-headers: Accept,Content-Type,Origin,Referer,User-Agent
access-control-allow-methods: GET, POST, PUT, DELETE, OPTIONS
access-control-allow-origin: *
cache-control: no-cache, private
content-type: application/json, text/plain, */*; charset=UTF-8
x-content-type-options: nosniff
x-ratelimit-limit: 60
x-ratelimit-remaining: 59
x-xss-protection: 1

which i have explicitly set on the application of domain2 (Laravel 5.8 application - CORS headers set in middleware).

But on iPhone, both Safari and Chrome (and on Safari browser on a Mac - Chrome works on Mac) I do not see any response - the conole.log(error) shows (see axios code below)

Error: Network Error

And in the network tab looking at the request/response there are no response headers returned and no HTTP status code. Only the request headers are shown.

My axios code is the following:

axios.post('https://domain2/test', formData)           
    .then(function (response) {

        console.log("POST function of axios 1");
        console.log(response);
        console.log("POST function of axios 2");
    })
    .catch(function (error) {
        console.log("Error in catch of axios post");
        console.log(error);
        console.log("End error");
    });

The formData is created using formData.append('a',12345) etc...

I can successfully POST to a test upload from https://domain1 to https://domain1 using the same axios code, so i believe there are some issues with the response headers from domain2 that iOS does not like and kills the response.

I've tried setting/changing all response headers, setting headers on the Axios POST, tried using simple xhr instead of Axios etc but to no avail...same error.

Anyone any pointers? I;ve googled etc... but have not found anything that helps. Even how i could get more information from the Error response on iPhone? I am debugging the iPhone on a Mac so can see the console.log etc...

Many thanks

Kenny Horna
  • 13,485
  • 4
  • 44
  • 71
user12345
  • 675
  • 1
  • 8
  • 17
  • What is your iOS version? – Dani R Jul 23 '19 at 11:40
  • @DaniR on the PC it is macOS High Sierra Version 10.13.6 and the iPhone 6 Plus is Ver 12.3.1 – user12345 Jul 23 '19 at 15:00
  • A view time ago i read something with iOS 12+ and the header. If i remember correctly you have to set the request header to `application/x-www-form-urlencoded`. But no warranty. – Dani R Jul 23 '19 at 15:09
  • Thanks @DaniR I'll try that and update when I get a chance. – user12345 Jul 23 '19 at 16:23
  • it may be cross origin error.. – ZeroOne Jul 28 '19 at 10:05
  • Thanks @ZeroOne - any pointers on a resolution? Since its a multipart form upload there is no preflight and the response headers, which look like they should work on iOS (and do on other browsers) are shown above – user12345 Jul 29 '19 at 13:17
  • I've seen before iOS having problems with wildcards. Try specifying `access-control-allow-origin: domain1` and be sure that all the headers sent by your request are also included on `access-control-allow-headers:` – Diogo Sgrillo Aug 01 '19 at 07:34

3 Answers3

1

Turns out this error was due to an Upgrade header apache was setting on the response. Once i unset that header in the apache config the issue was resolved on iOS. I set the following in the vhost of apache for the domain, in the directory section

Header unset Upgrade
user12345
  • 675
  • 1
  • 8
  • 17
  • check it [link](https://stackoverflow.com/questions/67883374/network-error-axios-trying-to-get-data-from-api) if you know that help me – MEAbid Jun 08 '21 at 13:34
0

adding the api domain in the info.plist fixed my problem:

  1. Open Xcode

  2. Click on your target

  3. Go to tab info (info.plist)

  4. find App Transport Security Settings

  5. Add a new field under it, and name it Exception Domains (if it does not exist already)

  6. under Exception Domains add a new field and name it YOUR_API_DOMAIN, e.g.: example.com

  7. Change its type to Dictionary

  8. Add a new field under your domain name and name it NSExceptionAllowsInsecureHTTPLoads

  9. Change its type to Boolean and specify its value as 1 (choose Yes)

  10. Close Metro bundler terminal and rebuild your application

For better understanding, see this screenshot:

info.plist configuration image

Mahdieh Shavandi
  • 4,906
  • 32
  • 41
-1

Is the mobile device and server on the same network? Use the servers local network IP and port instead of https://domain2. Replace my example IP with the servers IP and port below.

axios.post('http://10.0.0.12:8000/', formData)

I think its possible the mobile device throwing the error cant translate the url https://domain1?

Also try with https removed if you are using the servers IP. https://stackoverflow.com/a/48843551/4993755 Make sure OPTIONS is enabled in your servers cors settings.

Devin Norgarb
  • 1,119
  • 13
  • 18
  • Thanks but the POST request arrives OK at the server. It is the response from this request that is throwing this error – user12345 Jul 29 '19 at 07:51