我正在尝试使用angular js post请求向我的rails api发送帖子请求但我收到此错误:
index.php
我正试图找到一种方法如何使用angular to rails api发送帖子请求。我在rails中的控制器是这样的:
require_once('/home/sites/example.co.uk/public_html/retail/uploads/wordpress/wp-load.php');
require_once('/home/sites/example.co.uk/public_html/retail/uploads/wordpress/wp-blog-header.php');
这是我的角度要求:
XMLHttpRequest cannot load http://localhost:3000/api/v1/dis_generics. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:9000' is therefore not allowed access. The response had HTTP status code 404.
警告("失败消息:" + JSON.stringify({data:data})); });
这是我的应用程序控制器:
class DisGenericsController < ApplicationController
before_action :set_dis_generic, only: [:show, :edit, :update, :destroy]
skip_before_action :verify_authenticity_token
# GET /dis_generics
# GET /dis_generics.json
def index
# @dis_generics = DisGeneric.all
respond_to do |format|
format.html
format.json { render json: DisGenericDatatable.new(view_context) }
end
end
# GET /dis_generics/1
# GET /dis_generics/1.json
def show
end
# GET /dis_generics/new
def new
@dis_generic = DisGeneric.new
end
# GET /dis_generics/1/edit
def edit
end
# POST /dis_generics
# POST /dis_generics.json
def create
@dis_generic = DisGeneric.new(dis_generic_params)
respond_to do |format|
if @dis_generic.save
format.html { redirect_to @dis_generic, notice: 'Dis generic was successfully created.' }
format.json { render :show, status: :created, location: @dis_generic }
else
format.html { render :new }
format.json { render json: @dis_generic.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /dis_generics/1
# PATCH/PUT /dis_generics/1.json
def update
respond_to do |format|
if @dis_generic.update(dis_generic_params)
format.html { redirect_to @dis_generic, notice: 'Dis generic was successfully updated.' }
format.json { render :show, status: :ok, location: @dis_generic }
else
format.html { render :edit }
format.json { render json: @dis_generic.errors, status: :unprocessable_entity }
end
end
end
# DELETE /dis_generics/1
# DELETE /dis_generics/1.json
def destroy
@dis_generic.destroy
respond_to do |format|
format.html { redirect_to dis_generics_url, notice: 'Dis generic was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_dis_generic
@dis_generic = DisGeneric.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def dis_generic_params
params.require(:dis_generic).permit(:name, :is_combination, :rxcui, :status_id, :food_id, :hepatic_id, :renal_imp_id, :release_status_id, :is_essential)
end
end
答案 0 :(得分:1)
您需要在rails应用中添加访问控制标头,或者您可以根据此主题将方法更改为“JSONP”:AngularJS: No "Access-Control-Allow-Origin" header is present on the requested resource
var req = {
method: 'JSONP',
url: 'http://localhost:3000/api/v1/dis_generics',
headers: {
"Content-Type": "application/json"
},
data: {}
}
$http(req).success(function(data, status, header, config) {