this code is working fine, all that i want is to make ajax say please wait while still requesting for information: here is this code.
function products(){
if (document.getElementById('date').value.length == 0) {
alert("Please Pick Date");
document.getElementById('date').focus();
return false;
}
var ajaxRequest; // The variable that makes Ajax possible!
try{
// Opera 8.0+, Firefox, Safari
ajaxRequest = new XMLHttpRequest();
} catch (e){
// Internet Explorer Browsers
try{
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try{
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e){
// Something went wrong
alert("Your browser broke!");
return false;
}
}
}
// Create a function that will receive data sent from the server
ajaxRequest.onreadystatechange = function(){
if(ajaxRequest.readyState == 4){
var ajaxDisplay = document.getElementById('sum');
ajaxDisplay.innerHTML = ajaxRequest.responseText;
}
}
var usr = document.getElementById('usr').value;
var date = document.getElementById('date').value;
var queryString = "?usr=" + usr + "&date=" + date;
ajaxRequest.open("GET", "ajax_prom.php" + queryString, true);
ajaxRequest.send(null);
}
In your page, add a div with the id - divAjaxRequestInProgress. Add the text you want to show while the ajax request is in progress to this div. You could have an animated gif as well. Now, show this when you start your ajax processing and hide it once the request is complete.
function products(){
if (document.getElementById('date').value.length == 0) {
alert("Please Pick Date");
document.getElementById('date').focus();
return false;
}
$("#divAjaxRequestInProgress").show();
var ajaxRequest; // The variable that makes Ajax possible!
try{
// Opera 8.0+, Firefox, Safari
ajaxRequest = new XMLHttpRequest();
} catch (e){
// Internet Explorer Browsers
try{
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try{
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e){
// Something went wrong
alert("Your browser broke!");
return false;
}
}
}
// Create a function that will receive data sent from the server
ajaxRequest.onreadystatechange = function(){
if(ajaxRequest.readyState == 4){
var ajaxDisplay = document.getElementById('sum');
ajaxDisplay.innerHTML = ajaxRequest.responseText;
}
$("#divAjaxRequestInProgress").hide();
}
var usr = document.getElementById('usr').value;
var date = document.getElementById('date').value;
var queryString = "?usr=" + usr + "&date=" + date;
ajaxRequest.open("GET", "ajax_prom.php" + queryString, true);
ajaxRequest.send(null);
}
I have just added 2 lines to your code - one to show and one to hide the div.
Related
I need to send a ajax request to my server before web page close, my send code is below.
SendByAajx = function(msg) {
var response;
var xmlHttpReg;
if(window.XMLHttpRequest){
xmlHttpReg = new XMLHttpRequest();
} else if(window.ActiveXObject) {
xmlHttpReg = new ActiveXObject("Microsoft.XMLHTTP");
} else {
throw new Error("Unsupported borwser");
}
if(xmlHttpReg != null) {
xmlHttpReg.open("get", "https://127.0.0.1:57688/test"+'?'+msg, false);
xmlHttpReg.send(null);
if(xmlHttpReg.readyState==4){
if(xmlHttpReg.status == 200) {
var data = JSON.parse(xmlHttpReg.responseText);
if(typeof(data.errorcode) == "number" &&
data.errorcode != 0) {
throw("response error:" + data.errorcode);
}
response = data.result;
} else {
throw new Error("Error");
}
}
}
return response;
}
When I call this function in a button onclick event, it works.
function GetOnClick() {
try{
var result = SendByAajx (“data”);
} catch (e) {
//alert(errorInfo);
}
SetButtonDisabled(false);
}
But when I call this function when the page is unloaded, it doesn't work.
<body onload="javascript:OnLoad();" onunload="javascript:OnUnLoad()">
function OnUnLoad() {
try{
var result = SendByAajx(“data”);
} catch (e) {
//alert(errorInfo);
}
}
When I debug the application, the JS execution stops after this line:
xmlHttpReg.send(null);
It didn’t go to the next line:
if(xmlHttpReg.readyState==4)
The “data” is also not sent to the server.
What is wrong with my program, can ajax be called in an onunload function? What should I do to make it work?
<script type="text/javascript">
function fun1(){
var xmlHttp;
try {// Firefox, Opera 8.0+, Safari
xmlHttp = new XMLHttpRequest();
} catch (e) {// Internet Explorer
try {
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {
alert("Your browser does not support AJAX!");
return false;
}
}
}
xmlHttp.onreadystatechange = function(){
if (xmlHttp.readyState == 4) {
var respText = xmlHttp.responseText.split('<body>');
elem.innerHTML = respText[1].split('</body>')[0];
$("#st-content").show();
}
}
var ur='Tips.action';
xmlHttp.open("GET", ur, true);
xmlHttp.send(null);
}
</script>
Actually i have 2 jsps.one for inserting and another for viewing the inserted records.
The inserting jsp page appears in popup.after submitting the form the inserted record should be viewed in another jsp without page refresh.
How can i achieve this?please anyone guide me to complete this task
ajax success response you can replace response text to the popup current html code or a div code
I am running multiple ajax requests on a page, there is nothing wrong with the codes that I know of. It is just that when an ajax call is till running and the user chooses to hit reload or refresh, the browser will return an status error 0, in both ff3 and chrome 14 (IE6+ does not have this problem).
Since I have many request, the browser will alert multiple times and can be very annoying. I have read that this is a mozilla / firefox 3 bug, and it might have been fixed in later versions of ff, but chrome 14 apparently still has this error. But I am building for ff3 and chrome compatibility. There is a bug report seen Here
Does anyone know how to suppress the status error for reload / refresh only?
TIA
my ajax codes
function createXHR() {
var xhrObj;
if (window.XMLHttpRequest) {
// branch for native XMLHttpRequest object - Mozilla, IE7
try {
xhrObj = new XMLHttpRequest();
}
catch (e) {
alert("Your browser does not support this website!");
xhrObj = null;
}//close catche
} //close if
else if (window.ActiveXObject) {
// branch for IE/Windows ActiveX version
try {
xhrObj = new ActiveXObject("Msxml2.XMLHTTP");
} //try
catch(e) {
try{
xhrObj = new ActiveXObject("Microsoft.XMLHTTP");
}//inner try
catch(e) {
alert("Your browser does not support this website!");
xhrObj = null;
} //inner catch
}//catch
} //else if
return xhrObj;
}//eof createXHR
function search(var1, var2) {
var xhrObj = createXHR();
if (xhrObj) {
try {
var queryString = "whatever";
xhrObj.open("GET", "url.php"+queryString,true);
xhrObj.onreadystatechange = function (){callback(xhrObj,var1,var2);};
xhrObj.send(null);
} catch (e) {
alert ('search error');
return;
}
} else {
alert("search error has occured, please refresh and try again");
return; //do plan B. You do have a plan B, no?
}
}//tld close
function callback(xhr,var1, var2) {
if (xhr.readyState == 4) {
try {
if (xhr.status == 200)
{
var s = xhr.responseText;
//dosomething
}
else {
alert('Status error '+xhr.status);
return;
}
}
catch (e) {
alert("live Server Error ");
return;
//this will alert if a call is issued to a nonexistent function / variable
}
}
}//callback close
$(document).ready(function() {
function ajaxselectrss(rssurlvar) {
var ajaxRequest; // The variable that makes Ajax possible!
try{
// Opera 8.0+, Firefox, Safari
ajaxRequest = new XMLHttpRequest();
} catch (e){
// Internet Explorer Browsers
try{
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try{
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e){
// Something went wrong
alert("Your browser broke!");
return false;
}
}
}
// Create a function that will receive data sent from the server
ajaxRequest.onreadystatechange = function(){
if(ajaxRequest.readyState == 4){
var ajaxDisplay = document.getElementById('news');
ajaxDisplay.innerHTML = ajaxRequest.responseText;
}
}
//var rssurlvar = $(this).attr("title");
var queryString = "rurl=" + rssurlvar;
var urltofile = "rssget.php";
ajaxRequest.open("POST", urltofile, true);
ajaxRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
ajaxRequest.setRequestHeader("Content-length", queryString.length);
ajaxRequest.setRequestHeader("Connection", "close");
ajaxRequest.send(queryString);
}
$(".rshownews").click(function() {
window.setInterval(function() {ajaxselectrss($(this).attr("title"))}, 1000);
});
});
The POST query is "undefined" (Firebug).
You should use $.ajax - it will standardise the whole XmlHTTPRequest thing across browsers.
$.ajax({
type: "POST",
url: "rssget.php",
data: queryString,
success: function(data) {
$('#news').html(data);
}
});
(And, BTW, if you setInterval in your click handler, you will start a new periodic call to your ajaxselectrss function every time the button is clicked.)
Also, your context has changed due to the wrapper function. Try changing your click handler like so:
$(".rshownews").click(function() {
var _this = this;
window.setInterval(function() {ajaxselectrss($(_this).attr("title"))}, 1000);
});
Since you seem to use jquery anyway ($(document).ready) you could use it's wrapper for simplifying ajax-requests.
http://api.jquery.com/jQuery.post
I am creating an XMLHTTP object in Firefox and IE8 and i transfer a very large amount of data (json). In Firefox it renders it in 5 seconds, in IE8 it takes 30 seconds.
Is there any way to make IE8 XMLHTTP works faster?
Here is my code for creating the XHR object:
function createRequestObject2()
{
var http_request = false;
if(window.XMLHttpRequest)
{ //: Mozilla, Safari,...
http_request = new XMLHttpRequest();
if(http_request.overrideMimeType)
{
//: set type accordingly to anticipated content type
//http_request.overrideMimeType('text/xml');
}
}
else if(window.ActiveXObject)
{ //: IE
try
{
http_request = new ActiveXObject("Msxml2.XMLHTTP.3.0");
}
catch (e)
{
try
{
http_request = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e) { }
}
}
if(!http_request)
{
alert('Cannot create XMLHTTP instance');
return false;
}
return http_request;
}
Here is how i use the XHR:
http = createRequestObject2();
http.open('GET', encodeURI(action), true);
http.onreadystatechange = results;
http.send(null);
inwork = true;
viewLoading();
//: search results handling
function results()
{
/*console.log("do_search.results[ " +
'Results return ' + http.readyState
+ " ]"
);*/
if(http.readyState != 4)
return;
inwork = false;
if(http.status != 200)
{
//setStatusLine(httpapplicant.statusText);
return;
}
//: evaluate
var data = eval('(' + http.responseText + ')');
//: add data to the table
applicant_list = new dogtable(data, table1_options, $$('dog_results'));
hideLoading();
} //function results