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

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

问题描述

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

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'/>

当最终用户输入用户名时,我想检查数据库中是否存在此用户名不。如果它存在,我必须保持用户ID在隐藏字段,否则抛出一个错误。

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?

推荐答案

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

简体中文版:不,你做得不对。

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

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

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.

您有两个设施可供您使用,以使异步通信。首先:CFML代码写出文本,但是该文本可以是JS,浏览器然后在它最终接收它时运行。像:

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>

CFML服务器处理完之后,发送回浏览器的是:

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.

如果你需要使用JS代码在客户端与服务器通信,你唯一的(真实的)追索是将一个AJAX请求返回给服务器,将其传递给客户端信息,以便进一步的服务器端处理和服务器响应。

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提供了一些向导功能,写HTML和JS为了方便这一点,但总的来说这是一个糟糕的方法来实现这个目的,所以我不会推荐它。但是,我将指向一个为内置CFML巫术提供HTML / JS / CSS解决方案的项目: https: //github.com/cfjedimaster/ColdFusion-UI-the-Right-Way

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.

你需要考虑的是传递表单,这是非常好的原因,但是如果你修改你的方法,你可以实现你想要的结果。通过AJAX(jQuery使这很容易)返回到服务器,并在单独的请求中运行< cfquery> 代码。

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.

如果你阅读我从一开始就提到的博客文章(discloure:我写了,但我专门为这样的情况写了),那么你会明白为什么。

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天全站免登陆