如何将变量发送到带有ajax帖子的coldfusion操作页面? [英] How should I send variables to a coldfusion action page with ajax post?
问题描述
我的页面会从网页上的cfquery中生成随机变量,作为随机奖励和随机员工赢得奖品。
< cfset prizeID =#prize.prize_ID [variables.prizeRow]#>
然后我在页面上设置了cfform,其中我将文本输入设置为这些变量,代码将其提交到服务器操作页面,其中数据库更新我的表,表明奖金已声明:
function submitClaim
ColdFusion.Ajax.submitForm('claimyourprize','claim.cfm');
}
我试图找到一个替代品,我使用Ajax发送变量(prizeID,winnerID等)到服务器。
这里是我已经得到的:
(){
$ .ajax({
type:POST,
url:claim.cfm,
data:{claimedPrize:#prizeID#,claimedEmployee :#employeeID#}
})。done(function(){
alert(claimed);
})
}
目前我在按钮上调用该函数,点击声明奖品。
这是我的claim.cfm的一个查询:
< cfquery name =updateQuantitydatasource =圣诞节>
UPDATE PRIZES
SET QUANTITY = QUANTITY - 1
WHERE prize_ID = [客户发送的ID需要到这里]
< / cfquery>
您正在向claim.cfm页面提交表单。在claim.cfm页面上,您将具有表单范围。我建议你添加一些响应,所以你可以在客户端有某种结果。
function Claim(){
pre>
$ .ajax({
type:POST,
url:claim.cfm,
data:{claimPrize:#prizeID#,claimedEmployee:#employeeID #}
})。done(function(returnresult){
alert(returnresult);
})
}
这是claim.cfm页面
; cfif isDefined(form.claimedPrize)>
< cfquery name =updateQuantitydatasource =christmas>
UPDATE PRIZES
SET QUANTITY = QUANTITY - 1
WHERE prize_ID =< cfqueryparam value =#form.claimedPrize#cfsqltype =CF_SQL_INTEGER/>
< / cfquery>
成功!
< cfelse>
SOMETHING WENT错误!
< / cfif>
但更好的解决方案是使用cfc(component)unstead cfm(template) cffunction。
claim.cfc文件:
< cfcomponent displayName =我的声明组件>
< cffunction name =claimoutput =falseaccess =remotereturntype =string>
< cfargument name =claimedPrizerequired =truetype =numeric/>
< cfargument name =claimedEmployeerequired =truetype =numeric/>
< cfquery name =updateQuantitydatasource =christmas>
UPDATE PRIZES
SET QUANTITY = QUANTITY - 1
WHERE prize_ID =< cfqueryparam value =#arguments.claimedPrize#cfsqltype =CF_SQL_INTEGER/>
< / cfquery>
< cfreturnOK/>
< / cffunction>
< / cfcomponent>
然后ajax调用如下所示:
function Claim(){
$ .ajax({
type:POST,
url:claim.cfc?method = claim ,
data:{claimedPrize:#prizeID#,claimedEmployee:#employeeID#}
})。done(function(returnresult){
alert(returnresult);
})
}
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]#">
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'); }
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.
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>
解决方案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 ); }) }
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>
butmuch better solution is to have cfc (component) unstead cfm (template) and to submit form in to cffunction.
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>
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帖子的coldfusion操作页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!