Bug 52929 - No 'Access-Control-Allow-Origin'
Summary: No 'Access-Control-Allow-Origin'
Status: RESOLVED ANSWERED
Alias: None
Product: Forms
Classification: Xamarin
Component: Android ()
Version: 2.3.4
Hardware: Macintosh Mac OS
: --- normal
Target Milestone: ---
Assignee: Jimmy [MSFT]
URL:
Depends on:
Blocks:
 
Reported: 2017-03-01 14:07 UTC by Shay
Modified: 2017-03-14 18:04 UTC (History)
3 users (show)

Tags: android, webview
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED ANSWERED

Description Shay 2017-03-01 14:07:04 UTC
I am trying to load a local web page on Xamarin Forms using WebView.
The specific web page is trying to access server data using jQuery.
I am getting the following error (running on an Android device):

[chromium] [INFO:CONSOLE(0)] "XMLHttpRequest cannot load https://XXX. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.", source: file:///android_asset/ (0)

Shouldn't WebView allow such queries?
On iOS everything works fine.





------

=== Xamarin Studio Business ===

Version 6.2 (build 1821)
Installation UUID: 07360e74-2c80-443b-94a1-6221c776bc97
Runtime:
	Mono 4.8.0 (mono-4.8.0-branch/e4a3cf3) (64-bit)
	GTK+ 2.24.23 (Raleigh theme)

	Package version: 408000495

=== NuGet ===

Version: 3.5.0.0

=== Xamarin.Profiler ===

Version: 1.2.0
Location: /Applications/Xamarin Profiler.app/Contents/MacOS/Xamarin Profiler

=== Apple Developer Tools ===

Xcode 8.2.1 (11766.1)
Build 8C1002

=== Xamarin.iOS ===

Version: 10.4.0.123 (Xamarin Business)
Hash: 35d1ccd
Branch: cycle9
Build date: 2017-02-16 17:40:00-0500

=== Xamarin.Mac ===

Version: 3.0.0.393 (Xamarin Business)

=== Xamarin Inspector ===

Version: 1.1.2.0
Hash: cdc01b9
Branch: 1.1-release
Build date: Wed, 22 Feb 2017 23:24:11 GMT

=== Xamarin.Android ===

Version: 7.1.0.41 (Xamarin Business)
Android SDK: /Users/shayo/Library/Android/sdk
	Supported Android versions:
		4.0.3 (API level 15)
		4.4   (API level 19)
		5.0   (API level 21)
		5.1   (API level 22)
		6.0   (API level 23)
		7.0   (API level 24)
		7.1   (API level 25)

SDK Tools Version: 25.2.5
SDK Platform Tools Version: 25.0.3
SDK Build Tools Version: 25.0.1

Java SDK: /usr
java version "1.8.0_112"
Java(TM) SE Runtime Environment (build 1.8.0_112-b16)
Java HotSpot(TM) 64-Bit Server VM (build 25.112-b16, mixed mode)

Android Designer EPL code available here:
https://github.com/xamarin/AndroidDesigner.EPL

=== Xamarin Android Player ===

Version: 0.6.5
Location: /Applications/Xamarin Android Player.app

=== Build Information ===

Release ID: 602001821
Git revision: d41b6e51f3fa46a1943f2e31a778d28a7c73d069
Build date: 2017-02-17 15:18:19-05
Xamarin addins: 1363a8d943bab7700c93a97474060b6734aa7f94
Build lane: monodevelop-lion-cycle9

=== Operating System ===

Mac OS X 10.12.2
Darwin Shays-MacBook-Pro.local 16.3.0 Darwin Kernel Version 16.3.0
    Thu Nov 17 20:23:58 PST 2016
    root:xnu-3789.31.2~1/RELEASE_X86_64 x86_64
Comment 1 Jimmy [MSFT] 2017-03-01 16:11:28 UTC
Thank you for filing this report! You might be able to resolve this by using a custom WebView renderer to set setAllowUniversalAccessFromFileURLs[1] to true. Let me know if this helps!
    
[1] https://developer.android.com/reference/android/webkit/WebSettings.html#setAllowUniversalAccessFromFileURLs(boolean)
Comment 2 Shay 2017-03-01 16:44:13 UTC
Dear Jimmy,

Can you please post a full solution?
Thank you,

Shay
Comment 3 Jimmy [MSFT] 2017-03-01 17:33:30 UTC
I haven't tested this with a full app, but you can try a custom renderer like this: https://gist.github.com/jimmgarrido/b3a1bd8fc130435b53f62cb5736368a9
Comment 4 Shay 2017-03-02 04:25:55 UTC
Thanks Jimmy. It works. You can close this ticket. Please consider adding this as the default behavior (seems like this is the case for WebView in iOS).
Comment 5 Shay 2017-03-09 01:23:33 UTC
Minor comment. This only works if the server adds to the page the header 
'Access-Control-Allow-Origin', '*'
Is there a way to fix this for pages that I am not hosting?
Comment 6 Jimmy [MSFT] 2017-03-14 18:04:54 UTC
Glad to hear it is working now! Regarding the header question, unfortunately that does not appear to be possible unless you have access to the server: https://www.html5rocks.com/en/tutorials/cors/