2016-08-23 43 views
0

我試圖用ajax做一個「Post」,但我一直得到「400(Bad Request)」。我真的不知道它是否是rails網站或我的ajax請求。 導軌,它只是一個簡單的支架:獲取400(不良請求)使用Ajax到一個rails網站「POST」

# 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 

和我試圖讓工作阿賈克斯:

$("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); 
      } 


     }); 

回答

0

請改變你的

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

然後讓我知道結果。

謝謝