2017-01-13 39 views
0

我創建了跨平臺表單來提交數據。它以json格式發送數據,但rails控制器將其作爲html響應。所以我總是會得到錯誤。在跨平臺響應期間將json響應中的json響應更改爲html響應

Object {readyState: 0, responseJSON: undefined, status: 0, statusText: "error"} 

    test.html?batch[status]=0:88 

    Navigated to file:///home/shital/workspace/shitalluitel.github.io/test.html?batch%5Bstatus%5D=0 

這裏是我的軌登錄我的表格提交之後...

Started POST "/batches" for 127.0.0.1 at 2017-01-13 08:55:59 +0545 
    Processing by BatchesController#create as HTML 
     Parameters: {"batch"=>{"name"=>"eight", "course_id"=>"9", "start_date"=>"2016-12-12", "end_date"=>"2016-12-14", "status"=>"1"}} 
     User Load (0.7ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2 [["id", 1], ["LIMIT", 1]] 
     (0.2ms) BEGIN 
     Course Load (0.5ms) SELECT "courses".* FROM "courses" WHERE 

"courses"."id" = $1 LIMIT $2 [["id", 9], ["LIMIT", 1]] 
    SQL (0.4ms) INSERT INTO "batches" ("name", "course_id", "start_date", "end_date", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6) RETURNING "id" [["name", "eight"], ["course_id", 9], ["start_date", Mon, 12 Dec 2016], ["end_date", Wed, 14 Dec 2016], ["created_at", 2017-01-13 03:10:59 UTC], ["updated_at", 2017-01-13 03:10:59 UTC]] 
    (133.8ms) COMMIT 
entered to true part 
Completed 200 OK in 147ms (Views: 0.8ms | ActiveRecord: 135.6ms) 

回答

0

您可以使用

render json: {your_response: "value"}, status: 200 

,或者如果你什麼都不想要再

render :nothing => true 

你可以根據您的需要更改狀態。

+0

是什麼your_response和值這裏指 –

+0

這將是你的數據在控制器響應嘗試。如果有的話,你把你的數據放在那裏。就像考試的結果一樣,你在價值中對它進行了挖掘。 – bxorcloud

+0

什麼樣的數據?如果想發送批量模型的數據,那我該怎麼做?我也想檢查狀態? –

0

您可以將自動求出根據您的平臺

respond_to do |format| 
    format.html { render 'filename.html'} 
    format.json { render json: @batches } 
end 
+0

在哪個方法新建或創建... –

+0

respond_to可以用於任何行動取決於要求。對於你的情況,它在創建塊 –

+0

它沒有工作... –