首页 > 解决方案 > 在 Domino 中的 web 上提交()后字段未保存

问题描述

这与我几天前发布的一个问题有关。那里的答案使我发现了这个问题。

我在提交按钮中有以下代码

if (validateFields(document.forms[0])==true){
    if (validateAdditionalFields(document.forms[0])==true){         
        document.getElementById("Status").value = "Submitted for RFP";      
        getRespParty('ResponsibleParty');                   
        document.forms[0].submit();
    }
}

我在 JSHeader 的 getRespParty 函数中有以下代码

function getRespParty(x) {
var noEmployees = document.getElementById('NoEmployees').value;
var stateName = document.getElementById('State').value  
var url = 'http://' + window.location.host + 
'/ebsprospects.nsf/(GetResponsiblePerson)?OpenAgent&NoEmployees=' + 
 noEmployees + '&State=' + stateName;     
var xhttp = new XMLHttpRequest();
xhttp.open("GET", url);
xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {    
        document.getElementById(x).value = xhttp.responseText;  
    }
};
xhttp.send();
}

(GetResponsiblePerson) 代理以此代码结尾(我正在执行一系列 dbLookups 以返回特定的人,这就是为什么它必须在 LS 中)

Dim nam As NotesName
Set nam = session.Createname(respParty)
Print "Content-type:text/plain"    
Print nam.Abbreviated

字段责任方字段是文本、可见、可编辑,HTML 选项卡上的 ID 字段是责任方。当代码返回时,该字段显示我希望看到的名称。但是,它似乎并没有真正保存它,因为客户端文档在匹配的责任方字段中不包含任何内容。WQS 中没有代码。客户端中的责任方字段是隐藏和可编辑的,状态字段也是如此,它确实被保存了,所以至少我知道保存正在工作?

为什么代理返回的字段值不保存?逻辑类型甚至是可行的还是我需要以不同的方式来做?

标签: javascriptajaxlotus-noteslotus-dominolotusscript

解决方案


正如@umeli 所说,这是一个时间问题。您正在调用 getRespParty(),但只要调用 XMLHttpRequest,代码就会继续,退出函数并在 Ajax 调用完成之前执行提交。

恕我直言,一种更优雅的解决方案是使用来自 getRespParty() 的回调,并在那里调用提交:

document.getElementById("Status").value = "Submitted for RFP";      
getRespParty('ResponsibleParty', function() { document.forms[0].submit() }); 

然后修改getRespParty():

function getRespParty(x, callback) {
    var noEmployees = document.getElementById('NoEmployees').value;
    var stateName = document.getElementById('State').value  
    var url = 'http://' + window.location.host + 
    '/ebsprospects.nsf/(GetResponsiblePerson)?OpenAgent&NoEmployees=' + 
    noEmployees + '&State=' + stateName;     
    var xhttp = new XMLHttpRequest();
    xhttp.open("GET", url);
    xhttp.onreadystatechange = function() {
        if (this.readyState == 4 && this.status == 200) {    
            document.getElementById(x).value = xhttp.responseText; 
            if (callback != null && typeof callback === "function") {
                callback();
            }
        }
    };
    xhttp.send();
}

为了进一步改进,您可以使用不同的参数调用回调函数,具体取决于 XMLHttpRequest 是否成功:

if (this.readyState == 4) {
    if(this.status == 200) {    
        document.getElementById(x).value = xhttp.responseText; 
        if (callback != null && typeof callback === "function") {
            callback(true);
        }
    } else {
        if (callback != null && typeof callback === "function") {
            callback(true, this.status);
        }
    }
}

现在您可以读取该值并在主回调函数中执行不同的操作:

document.getElementById("Status").value = "Submitted for RFP";      
getRespParty('ResponsibleParty', function(success, statusCode) { 
    if (success) {
        document.forms[0].submit();
    } else {
        alert("Ajax call failed with status "+statusCode);
    }
});

每当您执行异步调用时,如果您计划在调用函数中执行操作,则应该使用回调。


推荐阅读