这是我的问题。我正在编写一个简单的应用程序,将数据发布到API。不幸的是我无法发布数据。每次尝试发布时,都会出现以下错误:
从源“ http://localhost:8000/api/v1/users”到“ http://localhost:4200”的XMLHttpRequest的访问已被CORS策略阻止:对预检请求的响应未通过访问控制检查:它没有HTTP正常状态。
(如果需要更多信息,请询问,我将其包括在内。这篇帖子开始变得很长,所以我删掉了一些代码。)
我当前的代码如下:
灰烬:
route路由/用户/add.js
import Route from '@ember/routing/route';
export default Route.extend({
model: function(){
return this.store.createRecord('user');
}
});
控制器/ controllers / users / add:
import Controller from '@ember/controller';
export default Controller.extend({
actions: {
saveUser: function(){
this.get('model').save().then(user => this.transitionToRoute('users.user', user));
}
}
});
模型模型/用户
从“ ember-data”导入DS;
export default DS.Model.extend({
username: DS.attr('string'),
password: DS.attr('string'),
firstName: DS.attr('string'),
lastName: DS.attr('string'),
roles: DS.attr('string'),
pic: DS.attr('string'),
});
查看视图/用户/添加
<div class="col-md-12">
<h4 class='form-label'><b>Add new pet</b></h4>
<form role="form" id="addUserForm">
<div class="form-group">
<label for="username">UserName:</label>
{{input class="form-control" id="username" value=model.username}}
</div>
<div class="form-group">
<label for="password">password:</label>
{{input class="form-control" id="password" value=model.password}}
</div>
<div class="form-group">
<label for="firstName">firstName:</label>
{{input class="form-control" id="firstName" value=model.firstName}}
</div>
<div class="form-group">
<label for="lastName">lastName</label>
{{input class="form-control" id="lastName" value=model.lastName}}
</div>
<div class="form-group">
<label for="roles">roles</label>
{{input class="form-control" id="roles" value=model.roles}}
</div>
<div class="form-group">
<label for="pic">pic</label>
{{input class="form-control" id="pic" value=model.pic}}
</div>
<button type="submit" class="btn btn-default" {{action 'saveUser'}}>Submit</button>
</form>
</div>
GoLang
main.go
import (
"github.com/rs/cors"
"log"
"net/http"
)
func main() {
r := NewRouter()
c := cors.New(cors.Options{
AllowedMethods: []string{"GET","POST", "PATCH", "DELETE", "OPTIONS"},
AllowedOrigins: []string{"*", "*", "*"},
AllowCredentials: false,
AllowedHeaders: []string{"application/json", "Content-Type, Access-Control-Allow-Origin"},
OptionsPassthrough: true,
Debug: true,
})
handler := c.Handler(r)
log.Fatal(http.ListenAndServe(":8000", handler))
}
模型/用户
type User struct {
Id int `jsonapi:"primary,users"`
Username string `jsonapi:"attr,username"`
Password string `jsonapi:"attr,password"`
FirstName string `jsonapi:"attr,firstname"`
LastName string `jsonapi:"attr,lastname"`
Role string `jsonapi:"attr,role"`
Pic string `jsonapi:"attr,pic"`
}
apiuserhandler.go
func ModifyUser(w http.ResponseWriter, r *http.Request) {
fmt.Println("Modify Handler")
jsonApiRuntime := jsonapi.NewRuntime().Instrument("/")
vars := mux.Vars(r)
fmt.Println(vars["id"])
id, err := strconv.Atoi(vars["id"])
if err != nil {
fmt.Println("Invalid User")
return
}
user := ReadUser(&models.User{Id: id})
if err := jsonApiRuntime.UnmarshalPayload(r.Body, user); err != nil {
http.Error(w, err.Error(), 500)
fmt.Println(err.Error())
return
}
UpdateUser(user)
if err := jsonApiRuntime.MarshalPayload(w, user); err != nil {
http.Error(w, err.Error(), 500)
}
w.WriteHeader(201)
w.Header().Set("Content-Type", "application/vnd.api+json")
}