我正在使用Jooby framework创建一个主要遵循此guide的API。我也使用Vue.js作为前端。但是我遇到了CORS问题。当我尝试从我的Vue.js前端执行get请求时,我收到错误否'Access-Control-Allow-Origin'标头出现在请求的资源上。因此,不允许原点“http://localhost:8081”访问。
这是我的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: []
}
这是我查询数据库的App.java文件
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);
}
}
我该如何解决这个问题?
答案 0 :(得分:1)