正如问题所述,我想从外部 API(谷歌地图 API)检索一些坐标并将其发送到我的 Controller 。目前我正在使用 jQuery/Ajax,但收到 500 内部服务器错误。我用 google 搜索了该错误,看来我可能正在发出跨域 ajax 请求。以下是相关的 javascript 和 Controller 代码:
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
}
});
Controller :
def calculate
x = params[:mydata]
#do something with x ...
end
是否可以将此 JavaScript 数据发送到我的 Controller ?我可以以某种方式使用隐藏表单提交数据吗?或者,如果我想在后端操作谷歌地图数据,我是否必须使用谷歌地图 gem 并在后端执行所有数据操作?
最佳答案
如果您希望从外部 API 获取数据,请考虑为此设置 ActiveResource。这样做将使数据可供您根据需要进行操作并提供给 Controller / View 。
查看此处了解更多详细信息: [http://api.rubyonrails.org/v3.2.1/classes/ActiveResource/Base.html]
或者您可以只使用已经构建的 Gem...
关于javascript - Rails - 如何将从外部 API 检索到的前端 javascript 数据发送到 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31035246/