CORS problem when using the API


#1

Hi all,
I’m developing a panel where there are quick Piwik statistics, now the problem is that Piwik is installed into server 1 and the other application is installed in local. When i attempt to use the API provided by Piwik with a jquery script it show CORSS error.

I tried this How do I configure my Piwik server to allow cross domain requests? (CORS) - Analytics Platform - Matomo but nothing changed.

The jquery script.


$.ajax({
		url: 'http://www.andreacatania.com/piwik/index.php',
		data: {   module: 'API'
				, method: 'Actions.getPageUrl'
				, idSite: 1
				, period: 'range'
				, date: 'last7'
				, pageUrl: pageUrl
				, format: 'json'
				, token_auth: '7917f2596f8bb70c954893f200ba6274'
				, crossDomain: true
			},
		type: 'get',
		dataType: 'json',
		success: function( data ){
			alert( data.sum_daily_entry_nb_uniq_visitors );
		},
		error: function(response, ajaxOptions, thrownError) {
			console.log( ajaxOptions + ': ' + thrownError );
		}
	});

The error returned by jquery script.


Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://www.andreacatania.com/piwik/index.php?module=API&method=Actions.getPageUrl&idSite=1&period=range&date=last7&pageUrl=%2Fcpumper%2Ftest&format=json&token_auth=7917f2596f8bb70c954893f200ba6274&crossDomain=true. This can be fixed by moving the resource to the same domain or enabling CORS.

What i have to do to avoid it?

Thanks in advance have a nice day, Byee.


(Matthieu Aubry) #2

Hi there, you can configure Piwik to allow Cross origin resource sharing, see this faq; How do I configure my Piwik server to allow cross domain requests? (CORS) - Analytics Platform - Matomo


#3

I’m facing the same issue as well. Eventhough I have cors_domains[] = * enabled, I still see the response header as -
Access-Control-Allow-Origin: https://localhost