获取400(错误请求)使用ajax到rails网站进行“POST”

时间:2016-08-23 19:41:21

标签: javascript ruby-on-rails ajax post

我正试图使用​​ajax制作一个“帖子”,但我不断收到“400(错误请求)”。我真的不知道这是rails网站还是我的ajax请求。 它只是一个简单的脚手架:

 # POST /events
 # POST /events.json
def create
 @event = Event.new(event_params)
respond_to do |format|
  if @event.save
    format.html { redirect_to @event, notice: 'Event was successfully created.' }
    format.json { render :show, status: :created, location: @event }
  else
    format.html { render :new }
    format.json { render json: @event.errors, status: 409 }
  end
end
end


def event_params
  params.require(:event).permit(:name, :consult, :description, :category, :likes)
end

和我正在努力工作的ajax:

$("button").click(function () {

        $.ajax({
            url: 'http://localhost:3000/events',

            type: 'POST',
            cache: true,
            processData: false,
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            data: { event: {  name: "testname", consult: 72, description: "this is a test", category: "dance", likes: 23 } },
            success: function (resp) {

                alert(resp);
            }


        });

1 个答案:

答案 0 :(得分:0)

请更改您的

url: http://localhost:3000/eventsurl: http://localhost:3000/events.json

然后让我知道结果。

由于