I am trying to create an api with Rails. When I try to issue an ajax request with json in the browser like this:
$.ajax({
type: "GET",
url: 'http://localhost:3000',
dataType: "json",
contentType: "application/json; charset=utf-8"
});
The rails server is looking at it like html, as can be seen from the logs:
Started GET "/" for ::1 at 2016-08-12 13:52:00 -0400
ActiveRecord::SchemaMigration Load (0.6ms) SELECT "schema_migrations".* FROM "schema_migrations"
Processing by LandingsController#index as HTML
It should say as JSON since the request is JSON.
It's depends on how your routing is set, not only what you pass in request. You should set default format in routes.rb
like so:
root 'your_root_controller#index', format: 'json'
Should work in Rails 4 & 5.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.