我应该如何使用 ajax post 将变量发送到coldfusion操作页面? [英] How should I send variables to a coldfusion action page with ajax post?

查看:19
本文介绍了我应该如何使用 ajax post 将变量发送到coldfusion操作页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的页面从页面上的 cfquery 生成随机变量,用作随机奖品和赢得奖品的随机员工.

My page generates random variables from a cfquery on the page to be used as a random prize and random employee who have won the prize.

<cfset prizeID="#prize.prize_ID[variables.prizeRow]#"> 

然后我在页面上有一个 cfform,我将文本输入设置为这些变量,并使用以下代码将其提交到服务器操作页面,在该页面中数据库更新我的表格,指示已领取奖品:

I then have a cfform on the page where I set the text inputs to these variables and use the below code to submit it to the server action page where the database updates my table indicating the prize is claimed:

function submitClaim() { 
ColdFusion.Ajax.submitForm('claimyourprize', 'claim.cfm');
}

我正在尝试找到一种替代方法,我使用 Ajax 将变量(prizeID、winnerID 等)发送到服务器.

I'm trying to find an alternative to this where I use Ajax to send the variables (prizeID, winnerID, etc) to the server.

这是我得到的最接近的:

Here is as close as I have gotten:

    function Claim() {
        $.ajax({
            type: "POST",
            url: "claim.cfm",
            data: { claimedPrize: "#prizeID#", claimedEmployee: "#employeeID#"}
        }).done(function( ) {
            alert( "claimed" );
        })
    }

目前我正在调用按钮点击函数来领取"奖品.

Currently I'm calling the function on button click to "claim" the prize.

这是我的 claim.cfm 中的一个查询:

Here is one of the queries on my claim.cfm:

    <cfquery name="updateQuantity" datasource="christmas">
    UPDATE PRIZES
    SET QUANTITY = QUANTITY - 1
    WHERE prize_ID = [ID sent from the client needs to go here]
    </cfquery>

推荐答案

您正在向 claim.cfm 页面提交表单.在 claim.cfm 页面上,您将拥有可用的表单范围.我建议您添加一些响应,以便您可以在客户端获得某种结果.

You are submiting form to claim.cfm page. On the claim.cfm page you will have form scope available. I would suggest You to add some response, so you can have some sort of result on client.

function Claim() {
    $.ajax({
        type: "POST",
        url: "claim.cfm",
        data: { claimedPrize: "#prizeID#", claimedEmployee: "#employeeID#"}
    }).done(function(returnresult) {
        alert( returnresult );
    })
}

这就是 claim.cfm 页面

And this would be the claim.cfm page

<cfif isDefined("form.claimedPrize")>
    <cfquery name="updateQuantity" datasource="christmas">
      UPDATE PRIZES
      SET QUANTITY = QUANTITY - 1
      WHERE prize_ID = <cfqueryparam value="#form.claimedPrize#" cfsqltype="CF_SQL_INTEGER" />
    </cfquery>
    SUCCESS!
<cfelse>
    SOMETHING WENT WRONG!
</cfif>

但更好的解决方案是使用 cfc(组件)而不是 cfm(模板)并将表单提交到 cffunction.

butmuch better solution is to have cfc (component) unstead cfm (template) and to submit form in to cffunction.

claim.cfc 文件:

claim.cfc file :

<cfcomponent displayName="My claim Component">
 <cffunction name="claim" output="false" access="remote" returntype="string">
     <cfargument name="claimedPrize" required="true" type="numeric"/>
     <cfargument name="claimedEmployee" required="true" type="numeric"/>
     <cfquery name="updateQuantity" datasource="christmas">
       UPDATE PRIZES
       SET QUANTITY = QUANTITY - 1
       WHERE prize_ID = <cfqueryparam value="#arguments.claimedPrize#" cfsqltype="CF_SQL_INTEGER" />
     </cfquery>
<cfreturn "OK" />
 </cffunction>
</cfcomponent>

然后 ajax 调用将如下所示:

Then ajax call would look like this :

function Claim() {
    $.ajax({
        type: "POST",
        url: "claim.cfc?method=claim",
        data: { claimedPrize: "#prizeID#", claimedEmployee: "#employeeID#"}
    }).done(function(returnresult) {
        alert( returnresult );
    })
}

这篇关于我应该如何使用 ajax post 将变量发送到coldfusion操作页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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