I am using Safari on my mac (v7.0.2) to test a web server on a PLC, and the function pvAccess.WritePV() in the PV_Access.js is called but it executes only sometimes, which I find really strange, the error message from the if statement doesn't come up, the debugger says its working.

Also when I used the debugger on the web inspector in Safari under the develop menu, and stepped through PV_Access.js it worked every time, but if i just play it doesn't work.

I call the function:

<button onclick = "check_value_text_box('track')" id = "Track_home" name = "Track_home">Track</button>

which is:

function check_value_text_box(next_page)
{
    if ((!Value.value) || (Value.value.length > 4) || (Value.value > 359) || (Value.value < 0))
    {
        alert("ERROR: Please enter a number between and including 0 and 359.");
    }
    else
    {
        pvAccess.WritePV('_offset', document.getElementById('Value').value);
        window.location.href = ''+ next_page +'.asp';
    }
}

and pvAccess.WritePV is defined in the file PV_Access.js:

pvAccess =  {}
pvAccess.Func = function() 
{
    function AccessPV(name, rValue, wValue)
    {
        var url = '/goform/ReadWrite';
        var data = 'redirect=/response.asp&variable=' + escape(name);
        if(rValue != null && rValue != "")
        {
            data += '&value=' + escape(rValue);
            data += "&write=1";
        }
        else
        {
            data += '&value=none';
            data += "&read=1";
        }
        var xmlHttp = null;
        try {
            // Mozilla, Opera, Safari sowie Internet Explorer (ab v7)
            xmlHttp = new XMLHttpRequest();
        } catch(e) {
            try {
                // MS Internet Explorer (ab v6)
                xmlHttp  = new ActiveXObject("Microsoft.XMLHTTP");
            } catch(e) {
                try {
                    // MS Internet Explorer (ab v5)
                    xmlHttp  = new ActiveXObject("Msxml2.XMLHTTP");
                } catch(e) {
                    xmlHttp  = null;
                }
            }
        }
        if (xmlHttp) 
        {
            xmlHttp.open('POST', url , 1);
            xmlHttp.onreadystatechange = function () {
                if (xmlHttp.readyState == 4) {

                    if(wValue != null)
                    {
                        wValue[3] = xmlHttp.responseText;
                        wValue[3] = wValue[3].replace("<!-- B&R ASP Webserver -->",""); 
                        // value attribut of node
                        wValue.value = wValue[3];
                        return wValue;
                    }


                }
            };
            xmlHttp.send(data);
        }
    }
    // public   
    {
        this.WritePV = function(name, value) 
        {
            AccessPV(name,value);
        }

        this.ReadPV = function(name,wValue)
        {
            return  AccessPV(name, null, wValue);
        }       
    }
}
pvAccess = new pvAccess.Func();

When I set a breakpoint on line 55 of PV_Access.js and let it play after it breaks, it works every time. The function takes in two inputs, the name of the variable and the value to which said variable will be set to.

有帮助吗?

解决方案

Because the issue appears to be caused by this line:

window.location.href = ''+ next_page +'.asp';

getting executed before your ajax call has completed, then the solution is to delay that line until the Ajax call has completed.

You can do that by adding a callback function to AccessPV so you can pass in a callback function that will be notified when the ajax operation is complete. Then, in WritePV(), pass in a callback function and change the window.location in that callback.

function check_value_text_box(next_page)
{
    if ((!Value.value) || (Value.value.length > 4) || (Value.value > 359) || (Value.value < 0))
    {
        alert("ERROR: Please enter a number between and including 0 and 359.");
    }
    else
    {
        pvAccess.WritePV('_offset', document.getElementById('Value').value, function() {
            window.location.href = ''+ next_page +'.asp';
        });
    }
}

pvAccess =  {}
pvAccess.Func = function() 
{
    function AccessPV(name, rValue, wValue, fn)
    {
        var url = '/goform/ReadWrite';
        var data = 'redirect=/response.asp&variable=' + escape(name);
        if(rValue != null && rValue != "")
        {
            data += '&value=' + escape(rValue);
            data += "&write=1";
        }
        else
        {
            data += '&value=none';
            data += "&read=1";
        }
        var xmlHttp = null;
        try {
            // Mozilla, Opera, Safari sowie Internet Explorer (ab v7)
            xmlHttp = new XMLHttpRequest();
        } catch(e) {
            try {
                // MS Internet Explorer (ab v6)
                xmlHttp  = new ActiveXObject("Microsoft.XMLHTTP");
            } catch(e) {
                try {
                    // MS Internet Explorer (ab v5)
                    xmlHttp  = new ActiveXObject("Msxml2.XMLHTTP");
                } catch(e) {
                    xmlHttp  = null;
                }
            }
        }
        if (xmlHttp) 
        {
            xmlHttp.open('POST', url , 1);
            xmlHttp.onreadystatechange = function () {
                if (xmlHttp.readyState == 4) {

                    if(wValue != null)
                    {
                        wValue[3] = xmlHttp.responseText;
                        wValue[3] = wValue[3].replace("<!-- B&R ASP Webserver -->",""); 
                        // value attribut of node
                        wValue.value = wValue[3];
                        return wValue;
                    }
                    if (fn) {
                        fn(xmlHttp.responseText);
                    }
                }
            };
            xmlHttp.send(data);
        }
    }
    // public   
    {
        this.WritePV = function(name, value, callback) 
        {
            AccessPV(name,value, null, callback);
        }

        this.ReadPV = function(name,wValue)
        {
            return  AccessPV(name, null, wValue);
        }       
    }
}
pvAccess = new pvAccess.Func();
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top