0

I'm trying to send a post with jQuery Ajax and it's working for desktop and Android devices but for some reason iOS devices are giving me an error..

$.ajax({
        method: "POST",
        url: myURL,
        mode: 'cors',
        data: {
            '0': 'data.list'
        },
        headers: {
            'Authorization': 'Basic ' + btoa(username + ':' + password),
        },
        success: function(response) {
            console.log(response);
        },
        error: function() {
            console.log("error");
        }
    });

So why is it not working for iOS devices? I mean, Ok if it did not work at all but it's working for Windows, Mac, Chrome, Safari, Android etc.

And I did allow Cors, headers etc. in my PHP code

The error messages I receive are

OPTIONS url1 Origin url2 is not allowed by Access-Control-Allow-Origin. XMLHttpRequest cannot load url1. Origin url2 is not allowed by Access-Control-Allow-Origin.

I removed the url, FYI.

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
BFDev
  • 9
  • 1
  • You said that it gives you error... so what is the error? – Dekel Aug 31 '17 at 09:45
  • My guess would be an issue in the `btoa()` logic, but seeing the error would help a lot here. Also note that jQuery's `$.ajax` has no `mode` property. – Rory McCrossan Aug 31 '17 at 09:50
  • @Dekel And of course i forget to show the errors.. I edited the first post – BFDev Aug 31 '17 at 09:52
  • 1
    Thanks for updating with the error. That indicates that you're attempting to make a cross domain request to a URL which does not return CORS headers. Therefore you will not be able to do this using JS as it's blocked by the Same Origin Policy. As a workaround you can check if the URL can be made to return JSONP, but this is highly unlikely. You would most likely need to user a server side proxy instead. – Rory McCrossan Aug 31 '17 at 10:04
  • Hm ok. But i mean it is working for desktop and Android, so it should not be a problem with CORS? The Access-Control-Allow-Origin is set on the requested resource. – BFDev Aug 31 '17 at 10:09
  • I just tried this on a iPhone 7 with iOS 10 (tried on iPhone 6S with iOS 9 before) and it is working! Could it be a problem with btoa() like you said before? – BFDev Aug 31 '17 at 10:11

0 Answers0