简体   繁体   English

Symfony2-如何使用JS从JS中的数据库(通过控制器)正确访问数据?

[英]Symfony2 - how to properly access data from the DB (via controller) in JS in order to use Flot?

I want to draw a chart using Flot. 我想使用Flot绘制图表。 The data is stored in my database (MongoDB) and Flot is being rendered in JS - so I need to somehow pass the data from my controller to Javascript in order to give this data to Flot. 数据存储在我的数据库(MongoDB)中,并且Flot正在JS中呈现-因此,我需要以某种方式将数据从控制器传递到Javascript,以便将数据提供给Flot。 I tried to make a restful api with FosRestBundle, but when i tried to call it from the javascript on document.ready, i got the following error: 我试图使用FosRestBundle创建一个宁静的api,但是当我尝试从document.ready上的javascript调用它时,出现以下错误:

XMLHttpRequest cannot load localhost:8000/ajax/loginsperday. 
Cross origin requests are only supported for protocol schemes:
http, data, chrome, chrome-extension, https, chrome-extension-resource.

What is the most efficient and proper way to draw a chart with Flot (JS) using the data from a database (MongoDB administered by Doctrine ODM)? 使用数据库中的数据(由Doctrine ODM管理的MongoDB),用Flot(JS)绘制图表的最有效,最正确的方法是什么?

you can : 您可以 :

  • use AJAX for load or reload data in your resolve web page or action on this (button, etc...) 使用AJAX在您的解析网页中加载或重新加载数据或对此操作(按钮等)。
  • add DOM element statically with your data from database that you have retrieve by your symfony controller doctrine. 通过Symfony控制器原理从数据库中静态添加DOM元素和数据。 After load you plugin JS with jquery for get those element DOM data for initiate your plugins (I already use this method for google chart) 加载后,您使用jquery插件JS获取那些元素DOM数据以初始化您的插件(我已经在Google图表中使用了此方法)

Don't forget to hide element DOM technical for just render a JS plugins 不要忘记隐藏仅用于呈现JS插件的元素DOM技术

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

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