[英]Change html response to json response in rails during cross platform response
I have created cross platform form to submit data. 我创建了跨平台表单来提交数据。 It sends data in json format, but rails controller response it as html.
它以json格式发送数据,但rails控制器将其作为html响应。 So that I always get error as.
这样我总是得到错误。
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
Here is my rails log after my form submit... 这是我提交表单后的Rails日志...
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)
You can use 您可以使用
render json: {your_response: "value"}, status: 200
or if you want nothing then 或者如果你什么都不想要
render :nothing => true
You can change the status base on you needs. 您可以根据需要更改状态。
you can try this in the controller response will automatically varies according to your platform 您可以在控制器中尝试此操作,响应会根据您的平台自动变化
respond_to do |format|
format.html { render 'filename.html'}
format.json { render json: @batches }
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.