Chrome控制台和rails日志中的输出不同

时间:2017-01-14 13:52:41

标签: jquery ruby-on-rails ajax

我正在尝试通过跨平台表单在rails中添加数据。数据也在添加,但我的浏览器中的响应始终是一些json对象的错误。

rails log中的输出: -

Started POST "/batches.json" for 127.0.0.1 at 2017-01-14 19:33:34 +0545
Processing by BatchesController#create as JSON
  Parameters: {"batch"=>{"name"=>"shitalluitel", "course_id"=>"9", "start_date"=>"2016-12-12", "end_date"=>"2016-12-14", "batch_status"=>"1"}}
  User Load (0.5ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2  [["id", 1], ["LIMIT", 1]]
   (0.2ms)  BEGIN
  SQL (0.5ms)  UPDATE "users" SET "current_sign_in_at" = $1, "last_sign_in_at" = $2, "sign_in_count" = $3, "updated_at" = $4 WHERE "users"."id" = $5  [["current_sign_in_at", 2017-01-14 13:48:34 UTC], ["last_sign_in_at", 2017-01-14 13:47:54 UTC], ["sign_in_count", 77], ["updated_at", 2017-01-14 13:48:34 UTC], ["id", 1]]
   (15.6ms)  COMMIT
   (0.2ms)  BEGIN
  Course Load (0.2ms)  SELECT  "courses".* FROM "courses" WHERE "courses"."id" = $1 LIMIT $2  [["id", 9], ["LIMIT", 1]]
  SQL (1.2ms)  INSERT INTO "batches" ("name", "course_id", "start_date", "end_date", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6) RETURNING "id"  [["name", "shitalluitel"], ["course_id", 9], ["start_date", Mon, 12 Dec 2016], ["end_date", Wed, 14 Dec 2016], ["created_at", 2017-01-14 13:48:34 UTC], ["updated_at", 2017-01-14 13:48:34 UTC]]
   (8.7ms)  COMMIT
Entered to true part
Completed 200 OK in 104ms (Views: 0.2ms | ActiveRecord: 36.8ms)

但在浏览器中显示如下: -

Object {readyState: 0, responseJSON: undefined, status: 0, statusText: "error"}
test.html?_ijt=m511tgfk5o03pohpjpk2ebuppf:88 
Navigated to http://localhost:63342/zeftware/shitalluitel.github.io/test.html?batch%5Bstatus%5D=0

提交表格的代码是: -

$(document).ready(function () {
        $('#submit-button').click(function() {
            $.ajax({
                type: "POST",
                url: "http://localhost:3000/batches.json",
                beforeSend: function(xhr) {xhr.setRequestHeader('X-CSRF-Token', $('meta[name="csrf-token"]').attr('content'))},
                xhrFields: {
                    withCredentials: true
                },
                dataType: "json",
                data: {     
                    batch: {
                      name: $("#name").val(),
                      course_id: $("#course_id").val(),
                      start_date: $("#start_date").val(),
                      end_date: $("#end_date").val(),
                      batch_status: $("#batch_status").val(),
                      }
                },
                error: function(error){
                    console.log(error);
                },
              success: function(data) {
                         console.log(data);
                         return false;
                      },
            })
        });
    })

我的控制器代码是: -

class BatchesController < ApplicationController
  skip_before_action :verify_authenticity_token
  def new
    @batch = Batch.new
    respond_to do |format|
      format.json
      format.html
    end
  end

  def create
    @batch = Batch.new(batch_param)
    respond_to do |format|
       if @batch.save
         puts "Entered to true part #{@batch.name}"
         format.html { redirect_to @batch, notice: "Save process completed!" }
         format.json { render json: @batch}
       else
         puts "entered to false part"
          format.html {
            flash.now[:notice]="Save proccess coudn't be completed!"
            render :new_batch
          }
          format.json { render json: @batch.errors}
      end
    end
  end
end

0 个答案:

没有答案