You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@cordova.apache.org by GitBox <gi...@apache.org> on 2022/07/07 15:20:30 UTC

[GitHub] [cordova-ios] breautek commented on issue #1241: cordova ios ajax call not working after upgraded to ios 6.2 (uiwebview to wkwebview)

breautek commented on issue #1241:
URL: https://github.com/apache/cordova-ios/issues/1241#issuecomment-1177781292

   Layout constraints is unrelated to AJAX/XHR requests in webviews.
   
   > getting this repsonse {"0":{"readystate":0,"status":0,"statustext":"error"},"1":"error","2":""} while ajax call
   
   This is consistent with [CORS](https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS) failure. I've talked about CORS on my [blog](https://breautek.com/articles/enabling-cors.html) quite extensively but the gist of it is:
   
   CORS is a security mechanism for browser users. Don't try to make sense of it, it's not a security mechanism for your app, and it doesn't really make much sense in cordova environments, however WKWebView enforces it and there is no API to disable it, so our only choice is to work around it.
   
   If you're making a XHR request to a remote server, the remote server must be configured to respond with the appropriate CORS headers, as well as the `OPTIONS` method version of any request.
   
   If you're making a XHR request to a local resource, there's a few different recommendations based on your circumstances:
   If possible, don't make XHR requests to local resources, and instead use the `file` plugin to read local files.
   
   If a framework that you're using is doing requests to local resources (common in angular frameworks), then I'd recommend using schemes. Using schemes will change the origin, and thus will change web storage containers. As a result, content in web storage including local storage or cookies will be inaccessible. **however**, if you're upgrading from UIWebView to WKWebView, you will face the same challenge anyway.
   
   To use schemes, simply add the following preferences to your `config.xml` file:
   
   ```xml
   <preference name="scheme" value="app" />
   <preference name="hostname" value="localhost" />
   ```
   
   Using schemes has several advantages and it might be worth doing this regardless as part of your UIWebView to WKWebView migration.
   
   If schemes is not an option for whatever reason, and you still need to use AJAX against local resources, then your last option is to rely on third-party plugins such as [Oracle's XHR fix plugin](https://github.com/oracle/cordova-plugin-wkwebview-file-xhr). These plugins generally works by rewriting the browser XHR implementation and redirects the request to the native side, where native requests aren't bound by CORS. This is the least ideal path, but is a path that was successful especially for those who used WKWebView for awhile and don't want the origin change that comes when migrating to schemes.
   
   Let me know if this helps or if you have any further questions.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@cordova.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@cordova.apache.org
For additional commands, e-mail: issues-help@cordova.apache.org