javascript - Using Ajax with JSF command button or form -
i might not tackling issue correctly, here going on. have few restful webservices want call. code call them in javascript. called so:
<h:body onload="smainit();"> <h:form onsubmit="smasignup();">
whenever page loads, make 2 ajax calls. these calls succeed. want make 2 more ajax calls whenever form submitted. however, these calls fail. not see errors firebug, stuck happening.
to elaborate on mean when fail, in netbeans, have breakpoints rest calls. hit breakpoints when onload event triggered. not hit breakpoints when onsubmit event triggered.
my theory right ajax calls dont work on page submit. correct? page changing cause ajax calls killed before can finish?
anyway, insight good. here javascript being called:
function geturlvars() { var vars = {}; var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m, key, value) { vars[key] = value; }); 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 else } } else if (window.activexobject) { // msie result = new activexobject("microsoft.xmlhttp"); } else { // no known mechanism -- consider aborting application } return result; } function createclientrecord(ip) { //get users id 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 callback: req.onreadystatechange = function() { if (req.readystate !== 4) return; // not there yet if (req.status !== 200) { // handle request failure here... return; } // request successful, read response var resp = req.responsetext; // ... , use needed app. }; var url = '/ui/webresources/signup'; post(req, clientinfo, url); } function mark(clientinfo) { var req = createrequest(); // defined above // create callback: req.onreadystatechange = function() { if (req.readystate !== 4) return; // not there yet if (req.status !== 200) { // handle request failure here... return; } // request successful, read response var resp = req.responsetext; // ... , use needed app. }; var url = '/ui/webresources/mark'; post(req, clientinfo, url); } function smainitgetip() { var req = createrequest(); // defined above // create callback: req.onreadystatechange = function() { if (req.readystate !== 4) { return; // not there yet } if (req.status !== 200) { // handle request failure here... return; } // request successful, read response var resp = req.responsetext; // ... , use needed 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 callback: req.onreadystatechange = function() { if (req.readystate !== 4) { return; // not there yet } if (req.status !== 200) { // handle request failure here... return; } // request successful, read response var resp = req.responsetext; // ... , use needed 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 id, if id isnt present, send null var temp = geturlvars()["said"]; if (temp === null || temp === 'undefined') { temp = null; } smasignupgetip(); }
you need stop default event fired (in our case submit event) return false;
in onsubmit
method.
with preventing synchronous postback occurring.
function smasignup() { //get id, if id isnt present, send null var temp = geturlvars()["said"]; if (temp === null || temp === 'undefined') { temp = null; } smasignupgetip(); //to prevent default event fired return false; }
Comments
Post a Comment