我可以在 JavaScript 中使用 ColdFusion 标签吗? [英] Can I use ColdFusion tags in JavaScript?

查看:15
本文介绍了我可以在 JavaScript 中使用 ColdFusion 标签吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以在 JavaScript 中使用 ColdFusion 标签吗?例如:

 <script language="javascript" type="text/javascript">功能验证用户(){var userName = document.getElementById("username");<CFQUERY DATASOURCE="mydatasourcename" NAME="getUser">从用户 u 中选择 USER_ID,COUNT(*)其中 u.firstname=userName;</CFQUERY><cfif getUser.recordCount EQ 0><!--- 显示 eroor 消息 ---><另类><!--- 将 userId 分配给隐藏字段 --->document.getElementById("userid").value=#USER_ID#</cfif>}</脚本>

当最终用户输入他们的用户名时,我想在数据库中检查该用户名是否存在.如果存在,我必须将用户 ID 保留在隐藏字段中,否则会引发错误.

我这样做正确吗?如果写错了,能否指点一下正确的方法?

解决方案

加长版:http://blog.adamcameron.me/2012/10/the-coldfusion-requestresponse-process.html

简短版:不,你做得不对.

中型 StackOverflow 友好版本:CFML 代码在请求的服务器端运行;JavaScript 在客户端浏览器上运行.需要明确的是:ColdFusion 服务器从不直接与浏览器通信:中间有一个 Web 服务器.客户端浏览器请求一个文件,Web 服务器被配置为将 .cfm 请求传递给 ColdFusion 服务器,然后它运行它的代码,将结果字符串(例如:HTML 网页)返回给 Web 服务器,然后 Web 服务器将其返回给浏览器.该 HTML 可能包含浏览器将执行的 JavaScript(内联或作为外部请求).

希望您可以从中看到服务器端代码和客户端代码之间没有直接交互.

不过,您可以使用两种工具来使两者异步通信.首先:CFML 代码写出文本,但该文本可以是浏览器最终接收到时运行的 JS.比如:

<cfset msg="G'day world"><script>alert("<cfoutput>#msg#</cfoutput>");</script>

CFML 服务器处理完之后,返回给浏览器的内容是:

<script>alert("G'day world");</script>

如果服务器端代码写出"数据作为其响应的一部分,那么服务器端代码数据可以在客户端进程中使用.上面的示例非常简单,并不是解决此问题的良好实践"方式,但它演示了该技术.

如果您需要在客户端使用 JS 代码与服务器进行回通信,您唯一(真正)的办法是向服务器发出 AJAX 请求,以将客户端信息传递给服务器,以进行进一步的服务器端处理和让服务器响应一些东西.解释如何最好地做到这一点超出了你的问题范围,但是有大量的信息可以做到这一点.

CFML 提供了一些向导"来编写 HTML 和 JS 以方便您完成此操作,但总体而言,这是实现此目的的不好方法,因此我不会推荐它.但是,我将向您指出一个为内置 CFML 向导提供 HTML/JS/CSS 解决方案的项目:https://github.com/cfjedimaster/ColdFusion-UI-the-Right-Way

回到简短的回答:不,你不能做你打算做的事情有很好的理由,但如果你修改你的方法,你可以实现你想要的目标.

您需要查看的是通过 AJAX 将表单字段传回服务器(jQuery 使这非常容易),并在单独的请求中运行您的 <cfquery> 代码.p>

如果您阅读了我从一开始就提到的那篇博客文章(披露:我写了它,但我是专门为这种情况写的),那么您就会明白为什么.

如果您在解决部分解决方案时遇到困难:提出另一个更关注您遇到困难的部分的问题.

Can I use ColdFusion tags in JavaScript? For example:

 <script language="javascript" type="text/javascript">
   function validateUser() {
    var userName = document.getElementById("username");

 <CFQUERY DATASOURCE="mydatasourcename" NAME="getUser">
  select USER_ID,COUNT(*) from  user u 
 where u.firstname=userName;
  </CFQUERY>
 <cfif getUser.recordCount EQ 0>
   <!--- Show eroor message --->
   <cfelse>
    <!--- Assign userId to hidden field --->
    document.getElementById("userid").value=#USER_ID#
  </cfif>   
    }
 </script>

<input type='textbox' name='username' id='username' onblur=validateUser()/>
<input type='hidden' name='userid' id='userid'/>

When the end user enters their username, I would like to check in a database if this username exists or not. If it exists, I have to keep the userid in the hiddenfield or else throw an error.

Am I doing this correctly? If it is wrong, could you suggest the correct way?

解决方案

Long version: http://blog.adamcameron.me/2012/10/the-coldfusion-requestresponse-process.html

Short version: no, you're not doing it right.

Mid-sized StackOverflow-friendly version: CFML code runs on the server side of a request; JavaScript runs on the client browser. And to be clear: the ColdFusion server never communicates with the browser directly at all: there's a web server in between. The client browser requests a file, the web server is configured to pass .cfm requests to the ColdFusion server, and it runs its code, returning the resulting string (eg: an HTML web page) to the web server which then returns that to the browser. That HTML might include JavaScript (inline or as external requests) which the browser will then execute.

Hopefully from that you can see that there's no direct interaction between server-side code and client-side code.

You have two facilities at your disposal to get the two communicating asynchronously though. Firstly: CFML code writes out text, but that text can be JS which the browser then runs when it finally receives it. Something like:

<cfset msg ="G'day world">
<script>alert("<cfoutput>#msg#</cfoutput>");</script>

Once the CFML server has processed that, what gets sent back to the browser is:

<script>alert("G'day world");</script>

In this way server-side code data can be used in client-side process if the server-side code "writes out" the data as part of its response. The example above is very trivial and not a "good practice" way of going about this, but it demonstrates the technique.

If you need to use JS code on the client to communicate back with the server, your only (real) recourse is to make an AJAX request back to the server to pass it client-side information for further server-side processing and for the server to respond with something. It is outwith the scope of your question to explain how best to do this, but there is a tonne of information out there to do this.

CFML provides some "wizards" to write HTML and JS out for you to facilitate this, but on the whole this is a bad approach to achieving this end, so I will not recommend it. However I will point you to a project which offers HTML/JS/CSS solutions to the inbuilt CFML wizardry: https://github.com/cfjedimaster/ColdFusion-UI-the-Right-Way

Back to the short answer: no, you cannot do what you are setting out to do for very good reasons, but if you revise your approach, you can achieve the ends that you want.

What you need to look at is passing the form fields back to the server via AJAX (jQuery makes this very easy), and run your <cfquery> code in a separate request.

If you read that blog article I mention from the outset (discloure: I wrote it, but I wrote it specifically for situations like this), then you'll understand why.

If you get stuck when working on part of your solution: raise another question more focused on whatever part you are stuck on.

这篇关于我可以在 JavaScript 中使用 ColdFusion 标签吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆