2017-11-29 4 views
1

Jooby frameworkのほとんどがこのguideに続くAPIを作成しています。私はフロントエンドとしてVue.jsも使用しています。しかし、私はCORSに問題があります。私がVue.jsフロントエンドからリクエストを取得しようとすると、エラーが発生します。いいえ'Access-Control-Allow-Origin'ヘッダーが要求されたリソースに存在します。 Origin 'http://localhost:8081'はアクセスできません。JoobyアプリケーションでCORSを正しく設定する方法

は、これは私のJoobyのapplication.confファイルです:

# add or override properties 
# See https://github.com/typesafehub/config/blob/master/HOCON.md for more 
details 
db = mem 

schema = """ 

create table if not exists pets (

id int not null auto_increment, 

name varchar(255) not null, 

primary key (id) 

); 
""" 
cors { 
# Configures the Access-Control-Allow-Origin CORS header. Possibly values: 
*, domain, regex or a list of previous values. 
# Example: 
# "*" 
# ["http://foo.com"] 
# ["http://*.com"] 
# ["http://foo.com", "http://bar.com"] 
origin: "*" 

# If true, set the Access-Control-Allow-Credentials header 
credentials: true 

# Allowed methods: Set the Access-Control-Allow-Methods header 
allowedMethods: [GET, POST] 

# Allowed headers: set the Access-Control-Allow-Headers header. Possibly 
values: *, header name or a list of previous values. 
# Examples 
# "*" 
# Custom-Header 
# [Header-1, Header-2] 
allowedHeaders: ["X-Requested-With, Content-Type, Accept, Origin"] 

# Preflight max age: number of seconds that preflight requests can be cached 
by the client 
maxAge: 30m 

# Set the Access-Control-Expose-Headers header 
# exposedHeaders: [] 
} 

そして、これは私がデータベース

package org.jooby.guides; 

import java.util.List; 

import org.jooby.Jooby; 
import org.jooby.Results; 
import org.jooby.jdbc.Jdbc; 
import org.jooby.jdbi.Jdbi; 
import org.jooby.json.Jackson; 
import org.skife.jdbi.v2.DBI; 
import org.skife.jdbi.v2.Handle; 

import com.typesafe.config.Config; 

public class App extends Jooby { 

{ 
use(new Jackson()); 

use(new Jdbc()); 

use(new Jdbi() 
    // 1 dbi ready 
    .doWith((final DBI dbi, final Config conf) -> { 
     // 2 open a new handle 
     try (Handle handle = dbi.open()) { 
     // 3. execute script 
     handle.execute(conf.getString("schema")); 
     } 
    })); 


/** Pet API. */ 
use("/api/pets") 
    /** List pets. */ 
    .get(req -> { 
     return require(DBI.class).inTransaction((handle, status) -> { 
     PetRepository repo = handle.attach(PetRepository.class); 
     List<Pet> pets = repo.list(); 
     return pets; 
     }); 
    }) 
    /** Get a pet by ID. */ 
    .get("/:id", req -> { 
     return require(DBI.class).inTransaction((handle, status) -> { 
     int id = req.param("id").intValue(); 

     PetRepository repo = handle.attach(PetRepository.class); 
     Pet pet = repo.findById(id); 
     return pet; 
     }); 
    }) 
    /** Create a pet. */ 
    .post(req -> { 
     return require(DBI.class).inTransaction((handle, status) -> { 
     // read from HTTP body 
     Pet pet = req.body(Pet.class); 

     PetRepository repo = handle.attach(PetRepository.class); 
     int petId = repo.insert(pet); 
     pet.setId(petId); 
     return pet; 
     }); 
    }) 
    /** Update a pet. */ 
    .put(req -> { 
     return require(DBI.class).inTransaction((handle, status) -> { 
     // read from HTTP body 
     Pet pet = req.body(Pet.class); 

     PetRepository repo = handle.attach(PetRepository.class); 
     repo.update(pet); 
     return pet; 
     }); 
    }) 
    /** Delete a pet by ID. */ 
    .delete("/:id", req -> { 
     return require(DBI.class).inTransaction((handle, status) -> { 
     int id = req.param("id").intValue(); 

     PetRepository repo = handle.attach(PetRepository.class); 
     repo.deleteById(id); 
     return Results.noContent(); 
     }); 
    }); 
} 

public static void main(final String[] args) { 
run(App::new, args); 
} 

} 

私はこれをどのように修正すればよいの照会App.javaファイルのですか?

+0

'allowHeaders'プロパティに' Access-Control-Allow-Origin'を追加する必要があると思います – Keerthivasan

答えて

0

documentationから、あなたはCorsHandlerを追加する必要があります:あなたは、デフォルト値を変更しない限り

{ 
    use("*", new CorsHandler()); 
    ... 
} 

プロパティは、オプションです。

関連する問題