javascript - Ajax call to different website returns html instead of an array -
i'm trying receive array using .ajax
request url.
here's controller, hosted locally on localhost:3000
def merchant_ids merchants = merchant.where(id: params[:id]).pluck(:merchant_name, :id, :merchant_city, :physical_address, :merchant_phone, :latitude, :longitude) render json: merchants end
if make request following way:
$.ajax('http://localhost:3000/dashboard/merchants/merchant_ids/1').done(function(data) { console.log(data); });
it logs array fine.
but if try same thing app using localhost:3001
returns
the whole html view instead of array.
what doing wrong here?
i have gem 'rack-cors'
, config:
config.middleware.insert_before actiondispatch::static, rack::cors allow origins '*' resource '/*', :headers => :any, :methods => [:get, :post] end end
in ajax call try putting contenttype , datatype json
try this-
$.ajax({ url:"http://localhost:3000/dashboard/merchants/merchant_ids/1", contenttype: "application/json; charset=utf-8", datatype: "json" }).done(function(data) { console.log(data); });
because trying access json data on callback, need specify datatype of returned data .. specify along ajax request..
datatype: "json",
Comments
Post a Comment