简体   繁体   English

将Ajax与JSF命令按钮或表单一起使用

[英]Using Ajax with JSF command button or form

I might not even be tackling this issue correctly, but here is what is going on. 我什至可能无法正确解决此问题,但这就是正在发生的事情。 I have a few RESTful webservices that I want to call. 我有几个要调用的RESTful Web服务。 The code to call them is in JavaScript. 调用它们的代码在JavaScript中。 It is called like so: 它的名称如下:

<h:body onload="smaInit();">
    <h:form onsubmit="smaSignUp();">

Whenever the page loads, I make 2 Ajax calls. 每当页面加载时,我都会调用2个Ajax。 These calls succeed. 这些调用成功。 I want to make 2 more Ajax calls whenever the form is submitted. 每当提交表单时,我都希望再进行2个Ajax调用。 However, these calls fail. 但是,这些调用失败。 I do not see any errors from Firebug, so I am stuck as to what is happening. 我看不到Firebug的任何错误,因此我无法确定发生了什么。

To elaborate on what I mean when I say they fail, in Netbeans, I have breakpoints for the Rest calls. 为了详细说明当我说它们失败时的意思,在Netbeans中,我有Rest调用的断点。 I hit the breakpoints when the onload event is triggered. 当触发onload事件时,我遇到了断点。 I do not however hit the breakpoints when the onsubmit event is triggered. 但是,当onsubmit事件被触发时,我没有达到断点。

My only theory right now is that Ajax calls dont work on a page submit. 我现在唯一的理论是Ajax调用在页面提交上不起作用。 Is this correct? 这个对吗? Does the page changing cause the Ajax calls to be killed before they can finish? 页面更改会导致Ajax调用在完成之前被终止吗?

Anyway, any insight would be good. 无论如何,任何见识都是好的。 Here is the JavaScript that is being called: 这是被调用的JavaScript:

function getUrlVars() { var vars = {}; 函数getUrlVars(){var vars = {}; var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m, key, value) { vars[key] = value; }); var parts = window.location.href.replace(/ [?&] +([^ =&] +)=([^&] *)/ gi,function(m,key,value){vars [key] =值;}); return vars; 返回变量 } }

function post(req, json, url)
{
    req.open("POST", url, true);
    req.setRequestHeader("Content-Type",
            "application/json");
    req.send(json);
}

function createRequest() {
    var result = null;
    if (window.XMLHttpRequest) {
        // FireFox, Safari, etc.
        result = new XMLHttpRequest();
        if (typeof result.overrideMimeType !== 'undefined') {
            result.overrideMimeType('text/xml'); // Or anything else
        }
    }
    else if (window.ActiveXObject) {
        // MSIE
        result = new ActiveXObject("Microsoft.XMLHTTP");
    }
    else {
        // No known mechanism -- consider aborting the application
    }
    return result;
}

function createClientRecord(ip)
{
    //get users id from url
    var id = getUrlVars()["said"];
    //get time
    var timeStamp = new Date().getTime();
    var url = [location.protocol, '//', location.host, location.pathname].join('');
    var map = {"userId": id, "ip": ip, "timeStamp": timeStamp, "url": url};

    return JSON.stringify(map);
}

function signUp(clientInfo)
{
    var req = createRequest(); // defined above
    // Create the callback:
    req.onreadystatechange = function() {
        if (req.readyState !== 4)
            return; // Not there yet
        if (req.status !== 200) {
            // Handle request failure here...
            return;
        }
        // Request successful, read the response
        var resp = req.responseText;
        // ... and use it as needed by your app.

    };

    var url = '/ui/webresources/signup';
    post(req, clientInfo, url);
}

function mark(clientInfo)
{
    var req = createRequest(); // defined above
    // Create the callback:
    req.onreadystatechange = function() {
        if (req.readyState !== 4)
            return; // Not there yet
        if (req.status !== 200) {
            // Handle request failure here...
            return;
        }
        // Request successful, read the response
        var resp = req.responseText;
        // ... and use it as needed by your app.

    };

    var url = '/ui/webresources/mark';
    post(req, clientInfo, url);
}


function smaInitGetIp()
{
    var req = createRequest(); // defined above
    // Create the callback:
    req.onreadystatechange = function() {
        if (req.readyState !== 4) {
            return; // Not there yet
        }
        if (req.status !== 200) {
            // Handle request failure here...
            return;
        }
        // Request successful, read the response
        var resp = req.responseText;
        // ... and use it as needed by your app.
        var clientInfo = createClientRecord(resp);
        mark(clientInfo);
    };

    var url = '/ui/webresources/ip';
    req.open("GET", url, true);
    req.send();
}

function smaSignUpGetIp()
{
    var req = createRequest(); // defined above
    // Create the callback:
    req.onreadystatechange = function() {
        if (req.readyState !== 4) {
            return; // Not there yet
        }
        if (req.status !== 200) {
            // Handle request failure here...
            return;
        }
        // Request successful, read the response
        var resp = req.responseText;
        // ... and use it as needed by your app.
        var clientInfo = createClientRecord(resp);
        signUp(clientInfo);
    };

    var url = '/ui/webresources/ip';
    req.open("GET", url, true);
    req.send();
}

function smaInit()
{
    var temp = getUrlVars()["said"];
    if (temp === null || temp === 'undefined')
    {
        //a social advertiser did not send them here
        return;
    }
    smaInitGetIp();
}


function smaSignUp()
{
    //get the id, if id isnt present, send null
    var temp = getUrlVars()["said"];
    if (temp === null || temp === 'undefined')
    {
        temp = null;
    }
    smaSignUpGetIp();
}

You need to stop the default event to be fired (in our case the submit event) with return false; 您需要使用return false;停止要触发的默认事件(在本例中为Submit事件) return false; in the onsubmit method. onsubmit方法中。

With this you are preventing synchronous postback to occurring. 这样可以防止发生同步回发。

function smaSignUp() {
    //get the id, if id isnt present, send null
    var temp = getUrlVars()["said"];
    if (temp === null || temp === 'undefined')
    {
        temp = null;
    }
    smaSignUpGetIp();
    //to prevent the default event to be fired
    return false;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM