我有,我把一些工作JS code中的 的部分我创建和编辑的意见,它的正常工作。
I have some working JS code which I put on the sections of my create and edit views, and it's working fine.
然而,当我试图将code到一个单独的JS文件中,code将不再调用控制器动作。
However, when I attempted to move the code to a separate JS file, the code would no longer call the controller action.
下面的JS code:
Here the JS code:
<g:javascript>
$(document).ready(function(){
<g:remoteFunction controller="project" action="ajaxGetClient" onSuccess="updateClient(data)"/>
});
function updateClient(data){
var element = $("#project\\.client");
element.empty();
element.val(data.name);
}
</g:javascript>
下面的控制器操作:
def ajaxGetClient = {
if(!params.id){
params.id = Project.find("FROM Project ORDER BY id").id
}
def projectInstance = Project.get(params.id)
render projectInstance?.client as JSON
}
和这里的普惠制code:
And here's the GSP code:
<g:textField name="project.client" id="project.client" maxlength="9" required="" disabled=""/>
<g:select id="project" name="project.id" from="${myPackage.Project.list()}" optionKey="id" required="" value="${productInstance?.project?.id}" class="many-to-one"
onchange="${
remoteFunction(
controller: 'project',
action: 'ajaxGetClient',
onSuccess: 'updateClient(data)',
params: '\'id=\' + this.value'
)}"
/>
我添加了一个资源 ApplicationResources.groovy
,改变了上述JS code到这一点:
I added a resource to ApplicationResources.groovy
and changed the above JS code to this:
<g:javascript library="updateclient"/>
我只是复制/粘贴的code到一个JS文件,然后得到了一个消息:
I simply copy/pasted the code into a JS file and then got a message:
未捕获的SyntaxError:意外的标记&LT;
这是我理解来自于它不承认普惠制的语法,所以我尝试了一些AJAX,我敢pretty的雏为:
which I understood came from it not recognizing the GSP syntax, so I tried some AJAX, which I'm pretty unexperienced at:
$(document).ready(function(){
$.ajax({
type: 'POST',
url: "${remoteFunction(controller:'project', action:'ajaxGetClient', onSuccess:'updateClient(data)')}"
});
});
下面就是我从浏览器控制台获取:
Here's what I'm getting from the browser console:
http://localhost:8080/MyApp/product/$%7BremoteFunction(controller:'project',%20action:'ajaxGetClient',%20onSuccess:'updateClient(data)')%7D 404(未找到)
坦率地说,我很茫然现在。任何帮助将是AP preciated。
Quite frankly, I'm at a loss right now. Any help would be appreciated.
我会在这里回答我的问题,因为它的大部分来自宝贵的意见,从我的一个朋友,但约书亚的回答也很重要,所以我结束了他们两人的完美组合。
I'll be answering my own question here, since the bulk of it came from valuable advice from a friend of mine, but Joshua's answer was also very important, so I ended up combining both of them.
这是我如何解决它:
在普惠制:
<script type="text/javascript">
var _URL = '${resource(dir: "")}/project/ajaxGetClient';
</script>
<g:javascript library="updateclient"/>
我使用的原因&LT;脚本&GT;
标签是因为,为了使 _url
变量成为在不同的文件可用,它必须在文件使用之前被声明。至少,这是什么其他的SO回答说:
The reason I'm using the <script>
tag is because in order for the _URL
variable to become usable across different files, it had to be declared before the file using it. At least that's what this other SO answer said:
Global在多个文件中的JavaScript变量
独立的JS文件:
$(document).ready(function(){
getClientAjax(null);
});
function getClientAjax(id) {
$.ajax({
url: _URL,
type: "POST",
data: { id: id },
success: function(data) {
updateClient(data);
}
});
}
function updateClient(data){
var element = $("#project\\.client");
element.empty();
element.val(data.name);
}
和控制器动作保持不变。
And the controller action remained the same.
最后,必须有创造了另一个JS功能,但我得说我对结果感到满意。
At the end, there had to be created another JS function, but I gotta say I'm happy with the result.
感谢所有帮助。