gpt4 book ai didi

javascript - 对不同网站的 Ajax 调用返回 html 而不是数组

转载 作者:行者123 更新时间:2023-12-03 09:32:20 25 4
gpt4 key购买 nike

我正在尝试使用对 URL 的 .ajax GET 请求来接收数组。

这是我的 Controller ,本地托管在 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

如果我通过以下方式提出请求:

$.ajax('http://localhost:3000/dashboard/merchants/merchant_ids/1').done(function(data) { console.log(data); });

它很好地记录了数组。

但是,如果我尝试使用 localhost:3001 从另一个应用程序执行相同的操作,它会返回 description

整个 html View 而不是数组。

我在这里做错了什么?

我有gem 'rack-cors' 和配置:

config.middleware.insert_before ActionDispatch::Static, Rack::Cors do
allow do
origins '*'
resource '/*', :headers => :any, :methods => [:get, :post]
end
end

最佳答案

在 Ajax 调用中尝试将 ContentType 和 dataType 设置为 json

试试这个-

    $.ajax({
url:"http://localhost:3000/dashboard/merchants/merchant_ids/1",
contentType: "application/json; charset=utf-8",
dataType: "json"
}).done(function(data) { console.log(data); });

因为您试图在回调中访问 json 数据,所以您需要指定返回数据的 dataType .. 还要与您的 ajax 请求一起指定它 ..

数据类型:“json”,

关于javascript - 对不同网站的 Ajax 调用返回 html 而不是数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31464891/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com