Code Tree
Code Tree

Reputation: 2209

rails - request treated as HTML not json

:( stuck here. Any help appreciated.

My log says


Started POST "/sign_in" for 127.0.0.1 at 2015-06-22 22:21:59 +0530 Processing by Users::SessionsController#create as HTML

but i try requesting json through jquery

$.ajax({

type: 'POST',
contentType: "application/json",
url: 'http://localhost:3000/sign_in',

...

my controller

class Users::SessionsController < Devise::SessionsController
 respond_to :json
def create

       respond_to do |format|
                format.html {
    .....
    }
        format.json {

    .....
        }

so what i am doing wrong here, i need to get the request as "#create as JSON"

Upvotes: 2

Views: 1297

Answers (2)

gabrielhilal
gabrielhilal

Reputation: 10769

Add a dataType to your ajax:

type: 'POST',
contentType: "application/json",
url: 'http://localhost:3000/sign_in',
dataType: "json"

EDIT

You are posting some data to the action create, so you should just manipulate the data inside the action and then redirect it to somewhere else.... You should not render any info in the action create as it is a post and not a get. For example:

def create
  # find your user based on the params
  my_user = User.find(params[:id])
  # you can manipulate the user or even save it in a session

  # redirect to the action show for example
  redirect_to user_path(my_user)
end

def show
  @user = User.find(params[:id])
  respond_to do |format|
    format.html
    format.json { render json: @user }
  end
end

In this way, if you access the url for the showaction with a .json you will get the user info in json.

Upvotes: 2

nesiseka
nesiseka

Reputation: 1318

Try adding dataType: "json" to your ajax request.

Upvotes: 0

Related Questions