繁体   English   中英

Rails-如何将从外部API检索到的前端javascript数据发送到控制器

[英]Rails - How to send front-end javascript data retrieved from an external API to the controller

如问题所述,我想从外部API(Google Maps API)中检索一些坐标并将其发送到我的控制器。 目前,我正在使用jQuery / Ajax,但出现500内部服务器错误。 我在错误中进行了搜索,看来我可能正在提出跨域Ajax请求。 以下是相关的JavaScript和控制器代码:

使用Javascript:

//google maps API code goes here. The points variable ultimately gives me an object with an array of coorindates. 
var points = polyline.GetPointsAtDistance(16000);

$.ajax({
    type: "POST",
    url: '/pages/calculate',
    mydata: points,
    success: function(response) {
    //do something with the response here
    }
});

控制器:

def calculate
    x = params[:mydata]
    #do something with x ...
end

是否可以将此JavaScript数据发送到我的控制器? 我可以以某种方式使用隐藏表单提交数据吗? 或者,如果我想在后端上操作Google Maps数据,是否必须使用Google Maps Gem并在后端上执行所有数据操作?

如果您希望从外部API获取数据,请考虑为此设置ActiveResource。 这样做将使数据可供您根据需要进行操作并提供给控制器/视图。

在此处查看更多详细信息:[ http://api.rubyonrails.org/v3.2.1/classes/ActiveResource/Base.html]

或者,您可以只使用已经建立的宝石...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM