gpt4 book ai didi

grails - grails-在remoteFunction属性中使用变量值

转载 作者:行者123 更新时间:2023-12-02 14:46:33 25 4
gpt4 key购买 nike

[请先发表评论,以查看此问题当前所在的位置,谢谢。希望快速/容易阅读]

我遇到了渲染模板的情况,并且模板具有javascript函数,需要通过remoteFunction将正确的值发送到 Controller Action ,例如下面:

<script type="text/javascript">
function updateOrderEntry() {
alert("updating OrderEntries");
var unused = ${remoteFunction(action:'userByName', id:userId, update:[success: 'userOrderList', failure: 'userOrderListError'])};
}
</script>

当此函数在页面上执行时,我得到了警报,并且可以“调用” Controller ,但是它没有发送id值(参数不包含id)。构建模板时,我将userId作为模型参数提供,并且我已验证它可以使它很好地进入模板(例如,在div中)。

我已经尝试了上述方法的一堆变体,但无法使其正常工作,例如使用remoteFunction作为标签:
 var unused = <g:remoteFunction action="userByName" id="${userId}" update="[success: 'userOrderList', failure: 'userOrderListError']" />;

我还使用params属性尝试了多种口味(这会更好),但是在没有使它起作用后,我想我会尝试使id参数起作用。

尝试可变地填充remoteFunction属性时,我在做什么错?

谢谢

[项目位于Grails 1.3.7上]

--------------------------------根据第1/2条评论进行更新------------ -----------------------

没有此 Controller 的URL映射,但是删除了我为另一个 Controller 所拥有的映射。 还将我在此 Controller 中进行的beforeInterceptor调试注释掉了,即:
  // def beforeInterceptor = [action: this.&debug]

// def debug() {
// println "ACTION: ${actionUri}, PARAMS: ${params}"
// }

我在userByName操作的顶部添加了调试代码,即:
 def userByName = {
println "action: ${actionUri}, params: ${params}"

我还做了3个版本的remoteFunction,它们是从模板中的 Controller 发回的,所有 3个都相同,但除外,第二个版本使用了硬编码的userId,而不是由变量产生的,即:
 function updateOrderEntry() {
alert("updating OrderEntries, 1st time");

var unused = ${remoteFunction(action:'userByName', id:userId, params:[userId:userId], update:[success: 'userOrderList', failure: 'userOrderListError'])};

alert("updating OrderEntries, 2nd time, THIS TIME WITH hardcoded id and param values");
var unused = ${remoteFunction(action:'userByName', id:'4', params:[userId:'4'], update:[success: 'userOrderList', failure: 'userOrderListError'])};

alert("updating OrderEntries, 3rd time, back to non-hard-coded values");
var unused = ${remoteFunction(action:'userByName', id:userId, params:[userId:userId], update:[success: 'userOrderList', failure: 'userOrderListError'])};
}

当此功能(从渲染模板)发送回浏览器时,浏览器(Chrome,使用其网络调试)显示它收到:
   function updateOrderEntry() {
alert("updating OrderEntries, 1st time");

var unused = jQuery.ajax({type:'POST',data:{'userId': '4'}, url:'/nameIsInAppPropsFile/money/userByName/4',success:function(data,textStatus){jQuery('#userOrderList').html(data);},error:function(XMLHttpRequest,textStatus,errorThrown){jQuery('#userOrderListError').html(XMLHttpRequest.responseText);}});;

alert("updating OrderEntries, 2nd time, THIS TIME WITH hardcoded id and param values");
var unused = jQuery.ajax({type:'POST',data:{'userId': '4'}, url:'/nameIsInAppPropsFile/money/userByName/4',success:function(data,textStatus){jQuery('#userOrderList').html(data);},error:function(XMLHttpRequest,textStatus,errorThrown){jQuery('#userOrderListError').html(XMLHttpRequest.responseText);}});;

alert("updating OrderEntries, 3rd time, back to non-hard-coded values");
var unused = jQuery.ajax({type:'POST',data:{'userId': '4'}, url:'/nameIsInAppPropsFile/money/userByName/4',success:function(data,textStatus){jQuery('#userOrderList').html(data);},error:function(XMLHttpRequest,textStatus,errorThrown){jQuery('#userOrderListError').html(XMLHttpRequest.responseText);}});;
}

所有这些看起来和我一样。 updateOrderEntry()被自动调用,并且我看到了所有3个警报,如上所示。相应地,在服务器上,我看到以下调试输出:
action: /money/userByName, params: [userId:null, action:userByName, controller:money]
action: /money/userByName, params: [userId:4, id:4, action:userByName, controller:money]
action: /money/userByName, params: [userId:null, action:userByName, controller:money]

问题是,在第一个和第三个remoteFunctions中没有正确设置userId或id(如在服务器上看到的),不清楚原因。有所不同,只是不知道是什么。

附言Chrome和Firefox使用相同的结果。

------根据以下注释3-5更新--------------------------

看看Chrome发送的内容,中间的好请求看起来像:
 Request URL:http://localhost:8080/nameIsInAppPropsFile/money/userByName/4
Request Method:POST
Status Code:200 OK
Request Headersview source
Accept:*/*
Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8
Connection:keep-alive
Content-Length:8
Content-Type:application/x-www-form-urlencoded
Cookie:JSESSIONID=F0FDCEE323A1834350E209A7C211E64D
Host:localhost:8080
Origin:http://localhost:8080
Referer:http://localhost:8080/nameIsInAppPropsFile/money/transaction
User-Agent:Mozilla/5.0 (Windows NT 6.0) AppleWebKit/535.7 (KHTML, like Gecko) Chrome/16.0.912.63 Safari/535.7
X-Requested-With:XMLHttpRequest
Form Dataview URL encoded
userId:4

2个不好的地方(注意第一行和最后一行,以及?)是:
 Request URL:http://localhost:8080/nameIsInAppPropsFile/money/userByName
Request Method:POST
Status Code:200 OK
Request Headersview source
Accept:*/*
Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8
Connection:keep-alive
Content-Length:11
Content-Type:application/x-www-form-urlencoded
Cookie:JSESSIONID=F0FDCEE323A1834350E209A7C211E64D
Host:localhost:8080
Origin:http://localhost:8080
Referer:http://localhost:8080/nameIsInAppPropsFile/money/transaction
User-Agent:Mozilla/5.0 (Windows NT 6.0) AppleWebKit/535.7 (KHTML, like Gecko) Chrome/16.0.912.63 Safari/535.7
X-Requested-With:XMLHttpRequest
Form Dataview URL encoded
userId:null

因此,这表明浏览器发送的内容有所不同。

当我硬编码给模板的值,即模型:[....,userId:'4']时,我得到了同样的失败结果。在使用之前:模型:[....,userId:order.user.id],但是我更新了调试输出,并确认即使在此处硬编码“4”也无济于事。

更新:添加与真实代码匹配的最小测试用例。注意:实际代码是一个包含3个部分的页面,顶部是固定的,其中选择了客户;中间部分会更新该客户的订单;底部显示了通过订单关联的信用卡交易,通常是多次交易(例如,授权,捕获,作废等)。当用户在底部交易部分中执行某项操作(例如无效)时,不仅需要更新该部分,还需要更新中间订单部分。我正在尝试获取订单部分-“ 第二 div”-通过使用底部交易部分上的link / remoteFunction并在其onComplete处理程序中进行调用,以执行第二个remoteFunction的Javascript函数进行更新,使中间订单部分更新。

下面是一个最小的测试案例,在该案例中,我无法完全重现该问题,但有趣地发现了一个不同但相关的问题。看起来Grails(或某种程度上是IntelliJ,我的IDE)在Javascript中时,在remoteFunction中指定的id参数有问题。运行它时,您会明白我的意思。希望可以快速复制代码并运行。谢谢。

----一些简单的 Controller ,不需要模型----
 class UiTestController {

static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

def index = {
println "rendering remoteTest"
render(view: 'remoteTest')
}

def section1 = {
println "action1 params: ${params}"

render(template: 'remoteSection1', model: [id: params.id])
}

def section2 = {
println "action2 params: ${params}"
render(template: 'remoteSection2', model: [id: params.id])
}
}

-remoteTest.gsp-
 <%@ page contentType="text/html;charset=UTF-8" %>
<html>
<head>
<title>Remote</title>
<g:javascript library="jquery" plugin="jquery"/>
</head>
<body>

<div style="text-decoration:underline; color:purple; cursor:pointer; margin: 20px"
onclick="${remoteFunction(action: 'section1', id:'1', update: 'section1')}">click to update section 1</div>
<br />
<div id="section1">1st Section to Update</div>
<br />
<div id="section2">2nd Section to Update</div>
</body>
</html>

---- _remoteSection1.gsp -----
  <g:javascript library="jquery" plugin="jquery"/>

<div>
<p>Section 1 updated, (id=${id}). Now, similar to real code, we
<span style="text-decoration:underline; color:purple; cursor:pointer;"
onclick="${remoteFunction(action: 'section2', id: '2', update: 'section2')}">update section 2</span>
</p>
</div>

---- _remoteSection2.gsp -----
 <g:javascript library="jquery" plugin="jquery"/>

<script type="text/javascript">
function updateSection1() {
alert("doing remoteFunction with fixed value 55");
var unused = ${remoteFunction(action: 'section1', id='55', update: 'section1')};
alert("doing remoteFunction with fixed value 65, using params");
var unused = ${remoteFunction(action: 'section1', params:[id:'65'], update: 'section1')};
}
</script>

<div>
<p>Section 2 updated, (id=${id}). Now, similar to real code, we
<span style="text-decoration:underline; color:purple; cursor:pointer;"
onclick="${remoteFunction(action: 'section2', id: '2', update: 'section2', onComplete: 'updateSection1()')}">update section 2 again, and also section 1 with javascript updateSection1() function</span>
</p>
</div>


FINAL UPDATE



代码现在可以使用,更改原因未知。我不得不继续执行其他一些任务,并且在更改 Controller 后又回到了这一任务,而且令人惊讶的是事情还在进行。与以前相同的Javascript调用。但是,如果您发现之前有些混乱,我会迅速将其发布:

-------------------------- _moneyEntries.gsp中的相关Java代码----------------- ---------
  function updateOrderEntry() {
alert("Updating OrderEntries -- 1st time");
${remoteFunction(action:'userByName', id:userId, update:[success: 'userOrderList', failure: 'userOrderListError'])};

alert("Updating OrderEntries -- 2nd time, THIS TIME WITH hardcoded id and param values");
${remoteFunction(action:'userByName', params:[id: '4'], update:[success: 'userOrderList', failure: 'userOrderListError'])};

alert("Updating OrderEntries -- 3rd time, back to non-hard-coded values");
${remoteFunction(action:'userByName', params:[id: userId], update:[success: 'userOrderList', failure: 'userOrderListError'])};

}

---------------- Google网络 View 向我显示的内容-整个回复--------------
<script type="text/javascript" src="/nameIsInAppPropsFile/plugins/jquery-       1.6.1.1/js/jquery/jquery-1.6.1.js"></script>

<script type="text/javascript">

function confirmVoid(moneyTransId) {
return confirm("Void transaction ID " +moneyTransId + ":");
}

function confirmCredit(moneyTransId, amountIn) {
var valid = false;
var amount = amountIn;
while (!valid) {
var r = prompt("For ID " +moneyTransId+ ", please specify the amount:", amount);
if (r == null || r == "") {
valid = true;
amount = -1;
} else {
//alert("result is:" +r);
if (r > amount) {
alert("You cannot credit an amount greater than " + amount);
amount = amountIn;
}
else {
amount = r;
// invoke remote function, update result
valid = true;
}
}
}
return amount;
}

function confirmCapture(moneyTransId) {
return confirm("Capture funds for ID " +moneyTransId + ":");
}

function confirmSend(moneyTransId) {
return confirm("Are you sure you want to send an email receipt for ID " +moneyTransId + ":");
}
//var unused = jQuery.ajax({type:'POST', url:'/nameIsInAppPropsFile/money/userByName/3',success:function(data,textStatus){jQuery('#userOrderList').html(data);},error:function(XMLHttpRequest,textStatus,errorThrown){jQuery('#userOrderListError').html(XMLHttpRequest.responseText);}});;

function updateOrderEntry() {
alert("Updating OrderEntries -- 1st time");
jQuery.ajax({type:'POST', url:'/nameIsInAppPropsFile/money/userByName/8',success:function(data,textStatus){jQuery('#userOrderList').html(data);},error:function(XMLHttpRequest,textStatus,errorThrown){jQuery('#userOrderListError').html(XMLHttpRequest.responseText);}});;

alert("Updating OrderEntries -- 2nd time, THIS TIME WITH hardcoded id and param values");
jQuery.ajax({type:'POST',data:{'id': '4'}, url:'/nameIsInAppPropsFile/money/userByName',success:function(data,textStatus){jQuery('#userOrderList').html(data);},error:function(XMLHttpRequest,textStatus,errorThrown){jQuery('#userOrderListError').html(XMLHttpRequest.responseText);}});;

alert("Updating OrderEntries -- 3rd time, back to non-hard-coded values");
jQuery.ajax({type:'POST',data:{'id': '8'}, url:'/nameIsInAppPropsFile/money/userByName',success:function(data,textStatus){jQuery('#userOrderList').html(data);},error:function(XMLHttpRequest,textStatus,errorThrown){jQuery('#userOrderListError').html(XMLHttpRequest.responseText);}});;

}


</script>

<div>
<div style="margin-bottom:9px;">Order # 1005 Merchant Transactions:
<span class="jsAction" style="padding-left:30px"
onclick="jQuery.ajax({type:'POST',data:{'userId': '8'}, url:'/nameIsInAppPropsFile/money/userByName',success:function(data,textStatus){jQuery('#userOrderList').html(data);},error:function(XMLHttpRequest,textStatus,errorThrown){jQuery('#userOrderListError').html(XMLHttpRequest.responseText);}});; return false;">(refresh order status)</span></div>
<div class="list">
<table>
<thead>
<tr>
<th>What Action?</th>
<th class="sortable" ><a href="/nameIsInAppPropsFile/money/voidTrans/12?sort=id&amp;order=asc">ID</a></th>

<th class="sortable" ><a href="/nameIsInAppPropsFile/money/voidTrans/12?sort=dateCreated&amp;order=asc">Date</a></th>

<th class="sortable" ><a href="/nameIsInAppPropsFile/money/voidTrans/12?sort=type&amp;order=asc">Transaction Type</a></th>

<th class="sortable" ><a href="/nameIsInAppPropsFile/money/voidTrans/12?sort=responseCode&amp;order=asc">Result</a></th>

<th class="sortable" ><a href="/nameIsInAppPropsFile/money/voidTrans/12?sort=totalAmt&amp;order=asc">Amount</a></th>

<th class="sortable" ><a href="/nameIsInAppPropsFile/money/voidTrans/12?sort=description&amp;order=asc">Description</a></th>

<th class="sortable" ><a href="/nameIsInAppPropsFile/money/voidTrans/12?sort=txid&amp;order=asc">Authorize.Net Transaction Id</a></th>

<th class="sortable" ><a href="/nameIsInAppPropsFile/money/voidTrans/12?sort=responseReasonText&amp;order=asc">Authorize.Net Response Text</a></th>

<th class="sortable" ><a href="/nameIsInAppPropsFile/money/voidTrans/12?sort=ccNumber&amp;order=asc">Credit Card #</a></th>

</tr>
</thead>
<tbody>

<tr class="odd">

<td>
<!-- First Check for transaction validity, for it to be 'actionable' -->

none

</td>


<td>13</td>

<td>01-13-12 03:41 MST</td>

<td>VOID OF previous authorization, ID 12</td>

<td><span class="">
Approved</span></td>

<td>NA</td>

<td>Void: sfk .</td>


<td>2168161109</td>

<td><span class="">
This transaction has been approved.</span></td>

<td></td>

</tr>

<tr class="even">

<td>
<!-- First Check for transaction validity, for it to be 'actionable' -->
none
</td>


<td>12</td>

<td>01-13-12 03:41 MST</td>

<td>AUTHORIZATION ONLY - then Voided in ID 13</td>

<td><span class="">
Approved</span></td>

<td>$11</td>

<td>sfk .</td>


<td>2168161109</td>

<td><span class="">
This transaction has been approved.</span></td>

<td>x9113</td>

</tr>

</tbody>
</table>
</div>

<!--div class="paginateButtons">

</div-->
</div>

--------------------------调试输出在 Controller 中显示的内容-----
 ACTION: /money/userByName, PARAMS: [id:8, action:userByName, controller:money]
ACTION: /money/userByName, PARAMS: [id:4, action:userByName, controller:money]
ACTION: /money/userByName, PARAMS: [id:8, action:userByName, controller:money]

因此,我不知道为什么现在而不是以前可以正常工作。但是updateOrderEntry()是相同的。也许您之前看到我做错了什么,在这种情况下,请告诉我。再次感谢您的所有帮助Tomasz,我学到了很多可以使用Google Chrome进行调试的功能(感谢您的帮助)。

最佳答案

对我来说,Grails 2.0很好用,我设置了def userId = 7,正如您所看到的,它出现在URL中:

            function updateOrderEntry() {
alert("updating OrderEntries");
var unused = jQuery.ajax({type:'POST',
url:'/controller/userByName/7',
success:function(data,textStatus)
{jQuery('#userOrderList').html(data);},
error:function(XMLHttpRequest,textStatus,errorThrown)
{jQuery('#userOrderListError').html(XMLHttpRequest.responseText);}
});;}

关于grails - grails-在remoteFunction属性中使用变量值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8720036/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com