Ajax request for server

Asked

Viewed 940 times

1

My minimum is the following, I am developing an API for a bakery, where it may be on the same server or different server, I am using the framework laravel in the version 5.2.*. In order to facilitate the work I made some controllers only on painel administrativo for example products. In my controllers I check if a json request is coming. This way I know if it’s a request or if it’s a view I should return, I programmed it this way:

 public function index(){
    $produtos =  Produto::all();

    if(Request::wantsJson()){
        return $produtos;
    }else{
        return view('Produto.listProduto', compact('produtos'));
    }
}

That being said and done all process and the same being tested, I’m trying to consume this data from another website, which in case would be the main site. My idea is to list all products and return them to a JSON.

In html I use JQUERY to make this request this way:

$.getJSON('http://localhost:8000/produtos', function(data) {
    console.log(data);
});

On my route in the painel administrativo I programmed it this way:

Route::singularResourceParameters();
Route::resource('produtos', 'ProdutoController');

With everything going wrong on console of my browser, use the google chrome but tested on others and gave the same kind of problem.

The exit was

Blocked cross-origin request: Same Origin Policy (Same Origin Policy) prevents reading the remote resource at http://localhost:8000/products. (Reason: CORS header 'Access-Control-Allow-Origin' is not present).

What can I do wrong ?

1 answer

1


Has to be installed via composer.phar a package for the release of CORS 'Access-Control-Allow-Origin', the barryvdh/Laravel-Cors (Cross-Origin Resource Sharing)

On the line of commando typhoon:

php composer require "barryvdh/laravel-cors"

in the archive config/app.php in providers add:

Barryvdh\Cors\ServiceProvider::class,

after installed type more this command line:

php artisan vendor:publish --provider="Barryvdh\Cors\ServiceProvider"

that will copy the configuration file to the briefcase config by the name of cors.php.

Finally in routes (route) put a middleware same example below:

Route::group(['middleware' => 'cors'], function(Router $router){
    $router->get('api', 'ApiController@index');
});

If you want to make an improved configuration do in the file config/cors.php, your standard is:

<?php
return [
    /*
     |--------------------------------------------------------------------------
     | Laravel CORS
     |--------------------------------------------------------------------------
     |
     | allowedOrigins, allowedHeaders and allowedMethods can be set to array('*')
     | to accept any value.
     |
     */
    'supportsCredentials' => false,
    'allowedOrigins' => ['*'],
    'allowedHeaders' => ['*'],
    'allowedMethods' => ['*'],
    'exposedHeaders' => [],
    'maxAge' => 0,
    'hosts' => [],
];

Links:

  • how would I do this ;

  • I made an issue.

  • Could not open input file: Composer.phar dropped this error in the log

  • I gave Poser require and it worked

  • I made a brief summary and sequence of commands @Renanrodrigues !

  • Only does the . phar change because here in mine it didn’t work this way, the other one went straight

  • What is your system?

  • my system is windows 10

  • should work but, all right!

  • actually is the name of the document, is Composer.json no Composer.phar

  • kkkkkkkkkkkkkkkk I imagine it was something wrong right there! because I use it because I use windows 10, but without problems install that step by step it will release for you! have settings I’ll put in answer!

Show 6 more comments

Browser other questions tagged

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