Slim Framework with Apache2.4

Asked

Viewed 61 times

0

Good morning. I am creating an API to provide a service for other applications. I have the following scenario. A system to provide the service. This system has a virtualhost configured. The Uri is http://meuservico.com.br/lib/api/api.php. When accessing this url, internally using get - the case I’ve tested at least - it prints the message on the screen.

$this->app->get("/", function () {
        $header = get_headers('http://www.meuservico.com.br', 1);
        $receive = $header['Content-Type'][0];
        $arr_receive = split(";", $receive);
        echo $arr_receive[0];
    });

I created an external project and this scrolls on localhost as well, but it does not have a virtualhost defined. Follow code for external access.

        $(function() {
        $.ajax({
            contentType: "application/json",
            url: 'http://meuservico.com.br/lib/api/api.php',
            data: { name: 'norm' },
            // dataType: "json",
            success: function(data){
                console.log(data);
            },
            error: function (request) {
                console.log(request);
            }
        });        
    });

In that case, I get the following error.

XMLHttpRequest cannot load http://meuservico.com.br/lib/api/api.php?name=norm. Response to preflight request doesn't pass access control check: The 'Access-Control-Allow-Origin' header has a value 'http://www.e-sms.com.br' that is not equal to the supplied origin. Origin 'http://localhost' is therefore not allowed access.

1 answer

0

This is not a problem with Apache and even with Slim, missing headers, if you have access to the file http://meuservico.com.br/lib/api/api.php you edit it add the headers, by error message it already has, but only releases pro http://www.e-sms.com.br, then change:

Access-Control-Allow-Origin: http://www.e-sms.com.br

For

Access-Control-Allow-Origin: *

However, if you don’t have access to the api.php file, then you can use jQuery itself by applying crossDomain: true, thus:

    $.ajax({
        contentType: "application/json",
        crossDomain: true,
        url: 'http://meuservico.com.br/lib/api/api.php',
        data: { name: 'norm' },
        // dataType: "json",
        success: function(data){
            console.log(data);
        },
        error: function (request) {
            console.log(request);
        }
    });

Note that this is not "real ajax", but it works quite similar.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.