是否可以在 javascript 文件(.js)中使用 web2py 语言?
我正在尝试将 AJAX 与 web2py 语言结合使用,但遇到问题 我的代码是:
javascript
$(document).ready(function(){
$(".className").click(function(){
jQuery.ajax({
type:'POST',
url:'getName',
data:{
itemName:'a'
},
timeout: 20000,
success: function(msg) {
alert(msg);
},
error: function(objAJAXRequest, strError){
alert( "Error:" + strError );
}
});
});
default.py
def getName():
itemName=request.vars.itemName
return "Name: " + itemName
问题是我想使用数据库中的数据,但是可以
{{for item in tableName:}}
var name={{=item.name}}
这样使用吗?
我不知道如何在 JavaScript 中从数据库中提取数据。 你能帮我一下吗? 干杯
I'm trying to use AJAX with web2py language but I have a problem
My code is:
javascript
$(document).ready(function(){
$(".className").click(function(){
jQuery.ajax({
type:'POST',
url:'getName',
data:{
itemName:'a'
},
timeout: 20000,
success: function(msg) {
alert(msg);
},
error: function(objAJAXRequest, strError){
alert( "Error:" + strError );
}
});
});
default.py
def getName():
itemName=request.vars.itemName
return "Name: " + itemName
The thing is I want to use the data from the database, but is it possible to use
{{for item in tableName:}}
var name={{=item.name}}
like this?
I'm not sure how to extract data from DB in javascript.
Can you help me a bit?
Cheers
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
简而言之,您无法使用 web2py 在 javascript 中直接从数据库中提取数据。您必须使用 web2py 查询数据库,然后使用 web2py 将查询数据发送到 javascript (或更准确地说,因为您使用的是 ajax,所以使用 jquery/javascript 从 web2py 中提取查询数据)。确保您实际上需要在客户端(javascript)执行逻辑,因为最简单的事情就是在 web2py 控制器中的 python 中执行所有逻辑。
但是,如果您出于某种原因需要在客户端对数据执行逻辑,那么您就走在正确的轨道上。对您来说最简单的事情是从 web2py 控制器中的数据库中获取您需要的记录,然后将记录打包为 web2py 控制器中的 json 对象(“import simplejson”,或者您可以使用标准库来完成此操作)最新版本的 python),然后返回该 json 对象,以便您的 js 使用上面包含的 ajax 请求来获取它。只有在那时,您才应该使用 javascript 循环遍历 json 对象来获取所需的数据。
也就是说,如果您只是尝试从数据库中的单个记录中获取字段,最简单的方法就是正确查询数据库以获取该记录,并将其返回到 ajax 脚本的 web2py 控制器中要得到。
The short answer is that you can't directly extract data from the db in javascript using web2py. You have to query the db with web2py, and then use web2py to send your query data to the javascript (or more accurately since you're using ajax, use jquery/javascript to pull your query data down from web2py). Make sure that you actually need to perform logic on the client (javascript) side here, because the easiest thing to do would be to perform all your logic in python in the web2py controller.
However, if you do for some reason need to perform logic on your data on the client side, then you are on the right track. The easiest thing for you to do would be fetch the records you need from the db in the web2py controller, then package the records up as a json object in your web2py controller ("import simplejson" or you can do it with the standard library with the latest version of python), then return that json object to make it available for your js to fetch using the ajax request you've included above. Only at that point should you loop through the json object using the javascript to get the data you need.
That said, if you're just trying to get a field from a single record from the database, the easiest thing to do would be just to query the database correctly to get that record, and return it in a web2py controller for your ajax script to get.