Thursday, January 12, 2012

Ajax not working in Google Chrome, Firefox

The Idea
Here will be the ajax XMLHttpRequest function. And how i implement this in my page. Only 2 function involve and this can be share by other page. Just matter on how you code your coding.

<script language="javascript">
var xmlhttp=null;
function getXMLHTTP() { //fuction to return the xml http object
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
catch (e){
// Internet Explorer
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
catch (e){
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
return xmlHttp;
function getAll(strURL,namaDiv) {
//alert (strURL);
var xmlhttp = getXMLHTTP();
if (xmlhttp) {

xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4) {
// only if "OK"
if (xmlhttp.status == 200) {
//alert(namaDiv + '\n' +req.responseText);
} else {
//alert("There was a problem while using XMLHTTP:\n" + req.statusText);
}"GET", strURL, true);
<a href="javascript:void(0);" onClick="getAll('ajax.php?jenis=logout&nilai=','divLogin')">Keluar</a>

The problem
My ajax working fine on hyperlink.. But i had the problem with button or any other input. I have retry to edit the ajax function, but the end still failed to work on Crome. Keeping browsing on the net for the solution but still no solution. But luckily..

The Solution
I have found the solution or the reason why the ajax failed to work on Crome & Firefox for the input button. You much ADD the <form name="ajax" method="POST" action=""></form> where the button and other input locate is. Yes just simple as that. This work on me, hope will work on you guys too. Good Luck!

Problem with AJAX and Chrome
Ajax XMLHttpRequest Test with Chrome
simple ajax works in IE but not chrome and FF
Problem with ajax onsubmit only in google chrome

No comments: