Im creating a API for my project, but im running into a issue. I'm working with Laravel 4.2. I've created a controller that returns a JSON string. Below my controller:
Controller:
public function license($value)
{
$array = ['test' => 'Nummer oneindigt'];
return json_encode($array);
}
After that I started to make my AJAX script:
//$site = "http://app.moldersmedia.nl/api/key/value/license.json";
$site = "http://app.dev/api/key/value/license.json";
$(document).ready(function() {
$.ajax({
url: $site,
crossDomain: true,
data: 'my_request_is=foo',
method: 'GET',
dataType: 'json',
contentType: 'application/json; charset=utf-8',
success: function(json){
alert('Request is geslaagd')
},
error: function(jqxhr, textStatus, error){
console.log(jqxhr);
console.log(textStatus);
console.log(error);
}
});
});
So after that i ran into some issues: the Access-Control-Allow-Origin
. After some research I figured out that i need to modify my .htaccess
, so thats what I did. Below my .htaccess
:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
<IfModule mod_headers.c>
Header add Access-Control-Allow-Origin "*"
Header add Access-Control-Allow-Headers "origin, x-requested-with, content-type"
Header add Access-Control-Allow-Methods "PUT, GET, POST, DELETE, OPTIONS"
</IfModule>
Now its working from 1 local environment to another with this code. So after that I uploaded my application to test online. This does not work yet. I am getting some errors. Can someone tell me what happens? You can see the errors of you paste my JavaScript in a HTML doc and uncomment the app.moldersmedia.nl link.
Im receiving the error:
XMLHttpRequest cannot load http://app.moldersmedia.nl/api/key/value/license.json?my_request_is=foo. The 'Access-Control-Allow-Origin' header contains multiple values 'http://default, *', but only one is allowed. Origin 'http://default' is therefore not allowed access.