AJAX Response not working in FF - ajax

I have a bit of code which alerts the $response from a php code in IE, but for some reason it doesn't work in FF..
Here's the code:
function contactfunction() {
var url = "scripts/php/cbb.php?pname=";
xmlHttp.open("GET", url, true);
xmlHttp.onreadystatechange = updatecontact1;
xmlHttp.send(null);
}
function updatecontact1() {
if (xmlHttp.readyState == 4) {
var response = xmlHttp.responseText;
alert(response);
}
}
And here's the PHP file (cbb.php)
<?php
$fp=fopen("ip.txt","a+");
fwrite($fp, "cib\r\n");
fclose($fp);
$response = "something";
return $response;
?>
Can anyone help me out here? I'm not sure how to get it to work in FF, it just gives a blank popup..

Do yourself a favour, and use one of the many ajax capable Javascript libraries out there, like jQuery, which frees the user from hacking up code to deal with browser discrepancies (for the most part, at least):
//using jQuery's $.get method
function update(name) {
$.get('cbb.php?name=' + name, function(response) {
alert(response);
$('#someDiv').html(response);
});
}
or:
//using jQuery's $.load method
function update(name) {
//directly inject div id="someDiv" with output of url
$('#someDiv').load('cbb.php?name=' + name);
}
Putting it together:
//when the DOM is ready
$(document).ready(function() {
//bind to click event of all anchors with class="contact"
$('a.contact').click(function(e) {
//stop the link from being followed
e.preventDefault();
//get the name from the link text
var name = $(this).text();
update(name);
});
});
Timmy
McLovin
<div id="someDiv">This is where the output will be injected</div>
See http://docs.jquery.com/Ajax for more information.

I managed to get the code working by using:
function getFile(fileToOpen) {
var xmlhttp;
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
} else if (window.ActiveXObject) {
// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
} else {
alert("Your browser does not support XMLHTTP!");
}
xmlhttp.open("GET",fileToOpen,false);
xmlhttp.send(null);
alert( xmlhttp.responseText );
}
function contactfunction() {
getFile('scripts/php/cbb.php?pname=');
}
If anyone else has the same problem :)

Related

get response from server without page refresh

<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

AJAX request not working in IE

$('.sizeSelect').change(function(size) {
sizeId = $(this).attr('id');
size = $('#' + sizeId).val();
lastChar = sizeId.substr(5);
addBtn = "#btn_" + lastChar;
shipId = "shipping_" + lastChar;
if (size=="")
{
document.getElementById(shipId).innerHTML="";
return;
}
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById(shipId).innerHTML=xmlhttp.responseText;
var response = xmlhttp.responseText;
if (response == 0.00) {
$(addBtn).addClass('invisible');
}
else if (response > 0.00) {
$(addBtn).removeClass('invisible');
}
}
}
xmlhttp.open("POST","cart.php?size="+size+"&shipId="+lastChar, true);
xmlhttp.send();
});
Can anyone tell me why this works in Chrome, Firefox and safari and not in IE. I read somewhere that it had to do with forcing ie not to cache and that changing the the request from a get to a post would help... it didnt :(
Any ideas?
Have you tried using jQuery? Have you checked how IE is handling the response - perhaps it sees "0.00" as text you have to parseFloat?
the trick is use separate code XML for IE browsers or that are version of less than 10 .
so every time Ajax is call a method parseXml is called with input parameter XML Dom or text, depending on browser .... and if current browser is IE, it upload XML doc, process it according to Microsoft standards and return XML and rest of processes in Ajax carries on as expected!!
note : browser.msie is not supported in jQuery 1.9 but you can add jquery-migrate-1.2.1.min.js in order to make it compatible or use userAgent and find which is current browser
$.ajax({
type: 'GET',
url: 'XML_file.xml',
dataType: ($.browser.msie) ? "text" : "xml",
success: function (xml) {
var processedXML = parseXml(xml);
$(processedXML).find('my record').each(function () { //code }
});
function parseXml(xml) {
if ($.browser.msie) {
if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
}
else {// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET", "XML_file.xml", false);
xmlhttp.send();
xmlDoc = xmlhttp.responseXML;
xml = xmlDoc;
}
return xml;
}

Rest webservice call for chrome browser

My first post
Im trying to call a rest webservice to populate my lov's dynamically using AJAX. It works fine for me in IE8 but not in chrome. I couldn't reach a particular line while executing that I mentioned in the below code. Am i missing anything. Please help.
07-24-2012 - I found the root cause for this issue. Its because of the application cache code specified in the html code. once i remove that the ajax call works fine. But my need is to make this page available in offline mode. Any suggestion on this to make the page available offline and also the ajax webservice calls works fine?. Thanks,
<script type="text/javascript">
var eleCount;
var xmlDoc;
function init() {
eleCount =0;
}
function populate()
{
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
alert("Window for chrome");
xmlhttp = new XMLHttpRequest();
}
else
{// code for IE6, IE5
alert("Else Window for IE");
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async = false;
}
var url = "http://localhost:7101/Mobile-MobileModel-context- root/jersey/Search/searchLov";
xmlhttp.onreadystatechange = function()
{
if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
var det = xmlhttp.responseText ;
if (window.DOMParser)
{
//I cannot reach this line while executing.
alert("This alert is not appearing");
var parser=new DOMParser();
xmlDoc=parser.parseFromString(det,"text/xml");
var x=xmlDoc.getElementsByTagName("fieldValue");
var txta =new Array();
....some code
}
}
else // Internet Explorer
{
alert("else window DOM parser for IE");
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async = false;
xmlDoc.loadXML(det);
...... some code
} // end for
} //end else for Other Browser (window.DOMParser)
} //end if Readystate 4 and Status 200
else{
}//else
}//close OnReadyStatechange Function
xmlhttp.open('GET',url,true);
xmlhttp.send(null);
eleCount = eleCount + 1;
}//end Function Populate()
</script>
I made it working -- The problem arises whenever i added this line
<HTML manifest=../../demo.appcache> This code will download the files mentioned in the manifest file to the browser's application cache. When I remove this line the rest calls are working fine.

Convert normal links to Ajax dynamically

I have put together some code from various sources but javascript is somewhat unknown to me and I only seem to fail with the code I have so far..
What I want to do is convert every normal link on the page to ajax links and load the pages through ajax.
So far I only succeeded to transform the links from the initial page and load the content in a div. The problem is I don't have access to the content is loading in the div and the new content still has normal links instead of ajax.
Is there a way I can convert the new links within the content loaded in the div, every time the div changes?
Also what I don't know is, if the user clicks on the home button in the menu, it will load the content of index along with this script, and everything will become a loop. How can I prevent the code from loading in index if it loads inside the div?
If you suspect any other problems it may occur from this code can you please advice me what to change?
This is the code I have so far..
Thank you very much for any advice!
Inserted in the head tag:
<script type="text/javascript">
var xmlhttp;
if(window.XMLHttpRequest) { xmlhttp=new XMLHttpRequest(); }
else if(window.ActiveXObject){ xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); }
else{ }
xmlhttp.onreadystatechange=function(){
if(xmlhttp.readyState==4)
{ document.getElementById("contentarea").innerHTML = xmlhttp.responseText; }
else{ document.getElementById("contentarea").innerHTML = ""; }
}
function loadPage(url){
document.getElementById("contentarea").innerHTML = "";
xmlhttp.open("GET",url,true);
xmlhttp.send(null);
}
</script>
Inserted somewhere in the body:
<div id="contentarea"></div>
Inserted right before the body ends:
<script>
var oP = document.getElementsByTagName("a");
var ctr=0;
while(ctr < oP.length){
var oldHref = document.getElementsByTagName("a")[ctr].href;
document.getElementsByTagName("a")[ctr].href="javascript:loadPage('"+oldHref+"');";
ctr++;
}
</script>
here ja go.. whipped this up for you.. you can use document.links to get all anchor tags. I'm doing this on 'contentArea' only. Everything inside (function () { will only be called when the document is ready. Then, in the onreadystatechange event I call the function again to set the click event on any anchors that are returned in the resposne.
<script type="text/javascript">
var xmlhttp;
function loadPage(url){
document.getElementById("contentarea").innerHTML = "";
xmlhttp.open("GET",url,true);
xmlhttp.send(null);
}
var parseAnchors = function(){
var anchors = document.getElementById("contentarea").links; // your anchor collection
var i = anchors.length;
while (i--) {
var anchor = anchors[i];
anchor.onclick = (function(url) {
return function() {
loadPage(url);
return false;
};
})(anchors[i].href);
anchor.href = "#";
}
};
(function () {
if(window.XMLHttpRequest) { xmlhttp=new XMLHttpRequest(); }
else if(window.ActiveXObject){ xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); }
else{ }
xmlhttp.onreadystatechange=function(){
if(xmlhttp.readyState==4)
{ document.getElementById("contentarea").innerHTML = xmlhttp.responseText; }
else{ document.getElementById("contentarea").innerHTML = ""; }
parseAnchors();
}
window.onload = function () {
parseAnchors();
}
</script>

AJAX call to ASP page to update database record works only once in IE9

After several pieces of helpful advice from the members of this forum I'm getting closer to solving what should be a simple issue. I'm a complete newbie to AJAX.
Can someone please tell me why this piece of code works fine in Firefox 8 but not in IE 9? It works ONCE in IE but not again unless I close the browser and reopen!!??
<script language="javascript" type="text/javascript">
<!--
//Browser Support Code
function ajaxFunction(){
var ajaxRequest;
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) {
}
}
ajaxRequest.open("GET", "disp_processTEXT.asp", false);
ajaxRequest.send(null);
}
//-->
</script>
The readystate is left blank as it is copied code which originally assigned a value to a div but I only want to run the database code in the asp page. Presumably this could have been deleted?
The code to be run in the asp page is
<%
Set MyConn = Server.CreateObject("ADODB.Connection")
MyConn.Open "dsn=xxx;uid=xxx;password=xxx;"
SQLString = "UPDATE dbo_tbl_printing_tempstore SET front_has_text1 = 'YES', front_text = 'help' WHERE id = 106567"
MyConn.Execute(SQLString)
MyConn.Close
Set MyConn = Nothing
%>
Please help me as this is driving me mad.
Many thanks
Switch to jQuery and its Ajax system as it should be cross browser compatable out the box, and saves you huge amounts of time:
Add a reference for jQuery:
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
and switch to its Ajax method (place below the above):
<script type="text/javascript">
$(document).ready(function() {
$.ajax({
url: 'disp_processTEXT.asp',
success: function(response) {
alert('It worked');
}
});
});
</script>
Update:
To bind to the onblur event using jQuery try this. Remove the onblur= attribute from your current code.
$(document).ready(function() {
$('#yourinput').on('blur', function(){
$.ajax({
url: 'disp_processTEXT.asp',
success: function(response) {
alert('It worked');
}
});
});
});
Demo: http://jsfiddle.net/qmPLj/
Try this. It has worked for me across all browsers but some Opera versions.
function GetXmlHttpObject(handler)
{
var objXmlHttp=null;
if(navigator.userAgent.indexOf("MSIE")>=0)
{
var strName="Msxml2.XMLHTTP";
if(navigator.appVersion.indexOf("MSIE 5.5")>=0)
{
strName="Microsoft.XMLHTTP";
}
try
{
objXmlHttp=new ActiveXObject(strName);
objXmlHttp.onreadystatechange=handler;
return objXmlHttp;
}
catch(e)
{
alert("Error. Scripting for ActiveX might be disabled");
return;
}
}
else //All other browsers
{
objXmlHttp=new XMLHttpRequest();
objXmlHttp.onload=handler;
objXmlHttp.onerror=handler;
return objXmlHttp;
}
}
var url= "http://www.example.com/";
xmlHttp=GetXmlHttpObject(stateChanged);
xmlHttp.open("GET", url , true);
xmlHttp.send(null);

Resources