0

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.
Donny
  • 1
  • 2
  • 1
    Noone will paste something in a code. Please provide error texts. – u_mulder Jul 18 '15 at 18:59
  • Im getting 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. – Donny Jul 18 '15 at 19:21
  • Did you change your htaccess on production too? – Ori Price Jul 18 '15 at 19:22
  • Yes i did. The .htaccess on the local and production environment are equal – Donny Jul 18 '15 at 19:23
  • The codes are btw also equal – Donny Jul 18 '15 at 19:27
  • See this: http://stackoverflow.com/questions/22343384/the-access-control-allow-origin-header-contains-multiple-values – Ori Price Jul 18 '15 at 19:36
  • Ori Price i've searched the internet a lot but i couldnt find my solution. I've checked the htaccess, server settings, controller functionality but nothing found that solves my issue – Donny Jul 18 '15 at 19:47
  • Could you paste here the headers that are being sent in your request? You can take it from developer tools F12 – Ori Price Jul 18 '15 at 20:01

0 Answers0