gpt4 book ai didi

javascript - ServiceNow Javascript 从 GlideAjax 函数传递变量

转载 作者:行者123 更新时间:2023-12-02 16:06:59 25 4
gpt4 key购买 nike

我正在使用 ServiceNow。我需要在提交时验证表单。我使用 GlideAjax 和脚本来验证数据。如何将变量从 Ajax 函数 calendarDate(response) 传递到客户端脚本中的其他函数?当glide ajax函数返回错误消息时,我想将变量“isValid”设置为false。

我已经使用不涉及 GlideAjax 的客户端脚本轻松完成了此操作。我只是将变量 isValid 设置为函数的结果,例如 var isValid = checkLnrDates();

但是,在使用 GlideAjax 时设置等于函数调用的变量不会返回任何我可以使用的值。我可能不理解 GlideAjax 函数的调用和处理方式。

目录客户端脚本 onSubmit

function onSubmit () {
var isValid = checkLnrDates();
if (isValid == false) {
g_form.submitted = false;
return false;
}
}


function checkLnrDates() {
var start = g_form.getValue('start_date');
//Check calendar date format valid YYYY-MM-DD
//Script include ClientDateTimeUtils checks the input data
var ajaxCalendarDate = new GlideAjax('ClientDateTimeUtils');
ajaxCalendarDate.addParam('sysparm_name', 'validateCalendarDate');
ajaxCalendarDate.addParam('sysparm_userDate', start);
ajaxCalendarDate.getXML(calendarDate);
}


function calendarDate(response){
//This is where we get the response returned from the ClientDateTimeUtils script include ajax function
var answer = response.responseXML.documentElement.getAttribute("answer");
if (answer != 'true'){
g_form.showFieldMsg('start_date', answer,'error');
//How can I pass the value of a variable to the function above? I want to set isValid to false
isValid = false;
return false;
}
}

最佳答案

事实上,您需要从 AJAX 往返中获取响应才能继续,这意味着您实际上并不是异步的。您可能只需调用 ajaxCalendarDate.getXMLWait(),然后调用 ajaxCalendarDate.getAnswer() 即可同步获取响应(请参阅 Synchronous GlideAjax)

但是,由于您已经提交,并且您的代码依赖于服务器端函数调用来验证某些输入,因此您可能只需考虑将此逻辑移至插入前业务规则中,以使用 进行验证和中止current.setAbortAction(true) 如果验证失败。维基示例here .

您的业务规则类似于:

function onBefore(current, previous) {
if (!CliendDateTimeUtils.validateCalendarDate(current.start_date)) {
current.setAbortAction(true); // Don't save the record
gs.addErrorMessage("Start date is not valid"); // Add an error message for the user
}
}

关于javascript - ServiceNow Javascript 从 GlideAjax 函数传递变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30629638/

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