Facebook App/Page Tab with xmlHttpRequest doesn't work in Firefox - ajax

I have e really big problem with firefox and facebook.
I mad an application on my webserver which uses xmlHttpRequest. I added this application to a facebook tab on a test facebook page. It works with IE, Chrome, Safari but not with firefox.
The request just keeps loading until timeout.
The JS functions i'm using:
function createXmlHttpRequest() {
try {
if (typeof ActiveXObject != 'undefined') {
return new ActiveXObject('Microsoft.XMLHTTP');
} else if (window["XMLHttpRequest"]) {
return new XMLHttpRequest();
}
} catch (e) {
changeStatus(e);
}
return null;
};
function downloadUrl(url, callback) {
var status = -1;
var request = createXmlHttpRequest();
if (!request) {
return false;
}
request.onreadystatechange = function() {
if (request.readyState == 4) {
try {
status = request.status;
} catch (e) {
}
if (status == 200) {
callback(request.responseXML, request.status);
request.onreadystatechange = function() {};
}
}
}
request.open('GET', url, f);
try {
request.send(null);
} catch (e) {
changeStatus(e);
}
};
function xmlParse(str) {
if (typeof ActiveXObject != 'undefined' && typeof GetObject != 'undefined') {
var doc = new ActiveXObject('Microsoft.XMLDOM');
doc.loadXML(str);
return doc;
}
if (typeof DOMParser != 'undefined') {
return (new DOMParser()).parseFromString(str, 'text/xml');
}
return createElement('div', null);
}
function downloadScript(url) {
var script = document.createElement('script');
script.src = url;
document.body.appendChild(script);
}
i call it through downloadUrl()
The Headers from the requested files:
header('Access-Control: allow <*>');
header("Access-Control-Allow-Origin: *");
header('Access-Control-Allow-Methods: GET');
header('Access-Control-Allow-Headers: X-PINGOTHER');
header("Content-type: text/xml");
i really tried everything, but it won't work in firefox...
what i've noticed: by observing firebug while loading this app in the facebook tab i could see that facebook is not requesting the adress given in the source, but other ones like: https://0-317.channel.facebook.com/pull?channel=p_1495135952&seq=389&partition=7&clientid=420773d2&cb=682&idle=0&state=active
i think it's surely firefox cross domain policy... but how can i solve this problem?
Anyone had the same problems ?
I thank you in advance.
Greetings

ok, found the problem.
Facebook is using UTF-8, but my page wasn't. So the stopped at the umlauts.
So it wasn't the Request at all.

Related

How can I send a synchronous ajax request to my server in onunload function

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?

Joomla 1.5 AJAX XMLHttpRequest Code Not Working in Joomla 2.5

Can someone tell me why this will not work in Joomla 2.5 but does in 1.5?
The error I'm receiving is - Uncaught TypeError: Cannot read property 'childNodes' of null ajax.php:33
If I enter in the url: index.php?option=com_mmg&controller=ajax&task=listModels&make_id=3
It outputs the ajxGetModels function like it's suppose to.
I know Joomla 2.5 Ajax Calls got changed or something like that but, I can't find a solution online to convert what I have below to the 2.5 version way of doing so.
Any help or direction would be very useful. Thank you.
function ajxGetModels(reload)
{
if (reload) {
var use_make = 'sel_make_id';
} else {
var use_make = 'make_id';
}
var make_id = document.getElementById(use_make).value;
var xhr = createXHR();
xhr.onreadystatechange = function() {
if (xhr.readyState == 4) {
if (xhr.status == 200) {
var model=document.getElementById("model_id");
try //Internet Explorer
{
xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async="false";
xmlDoc.loadXML(xhr.responseText);
}
catch(e)
{
try //Firefox, Mozilla, Opera, etc.
{
parser=new DOMParser();
xmlDoc=parser.parseFromString(xhr.responseText,"text/xml");
}
catch(e) {alert(e.message)}
}
var options =xmlDoc.getElementsByTagName("options").item(0);
model.innerHTML='';
for (i=0; i < options.childNodes.length; i++){
var newoption=document.createElement("option");
var myoption=options.childNodes[i];
var newtext=document.createTextNode(myoption.childNodes[0].nodeValue);
newoption.setAttribute("value",myoption.getAttributeNode("id").value)
newoption.appendChild(newtext);
model.appendChild(newoption);
}
document.getElementById('model_id').disabled=false;
document.getElementById('year_id').innerHTML='';
document.getElementById('year_id').disabled=true;
if (reload) {
var preVal = document.getElementById('sel_model_id').value;
setMmgVal('model_id',preVal);
ajxGetYears(true);
}
} else {
alert('Error code ' + xhr.status);
}
}
}
xhr.open("GET","index.php?option=com_mmg&controller=ajax&task=listModels&make_id="+make_id,true);
xhr.send(null);
}
function createXHR() {
var xhr = null;
if (window.XMLHttpRequest) {
xhr = new XMLHttpRequest();
} else if (window.ActiveXObject) {
try {
xhr = new ActiveXObject('Microsoft.XMLHTTP');
} catch (e) {}
}
return xhr;
}
It was not working because $error_reporting was set to "maximum" in configuration.php.
Still though - there has to be a better way.

ajax xhr.status = 0 errors after reload / refresh, how to prevent it?

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

Adapt ajax for crossdomain

Is it possible adapt this code for crossdomain and how
function makeRequest(url) {
var http_request = false;
if (window.XMLHttpRequest) { // Mozilla, Safari,...
http_request = new XMLHttpRequest();
if (http_request.overrideMimeType) {
http_request.overrideMimeType('text/xml');
}
} else if (window.ActiveXObject) { // IE
try {
http_request = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
http_request = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {}
}
}
if (!http_request) {
alert('Cannot create an XMLHTTP instance');
return false;
}
http_request.onreadystatechange = function() { alertContents(http_request); };
http_request.open('GET', url, true);
http_request.send(null);
}
function alertContents(http_request) {
if (http_request.readyState == 4) {
if (http_request.status == 200) {
receiveData(http_request.responseText);
} else {
alert("Îòâåò ñåðâåðà ïîëó÷åí, íî åñòü îøèáêà");
}
}
}
The same origin policy prevents JavaScript reading data from different origins under normal circumstances.
You can work around with:
A proxy for the data on the page's origin
JSONP
CORS (limited browser support, but possibly good enough for prime time now)

problem with AJAX request

i hava created the ajax XMLHttpRequest request for getting the data dyanmically ,
here is the code
var XMLHttpReq;
function createXMLHttpRequest() {
if (window.XMLHttpRequest) {
XMLHttpReq = new XMLHttpRequest();
} else {
if (window.ActiveXObject) {
try {
if(XMLHttpReq==null)
XMLHttpReq = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e) {
try {
if(XMLHttpReq==null)
XMLHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e) {
}
}
}
}
}
this is the method which sends the request
function personList(person) {
createXMLHttpRequest();
var url="query?option=person&userName="+person.innerHTML;
XMLHttpReq.open("GET", url, true);
XMLHttpReq.onreadystatechange =personListResponse;
XMLHttpReq.send(null);
}
function personListResponse() {
if (XMLHttpReq.readyState == 4) {
if (XMLHttpReq.status == 200) {
var xml=XMLHttpReq.responseXML;
}
}
}
the request is sent to the servlet only for the first time,when i try for the second the request is not sent ,instead am getting the previous response what i got earlier
I suppose it's cache.
Try adding this before the request:
XMLHttpReq.setRequestHeader("Cache-Control", "no-store, no-cache, must-revalidate");
XMLHttpReq.setRequestHeader("Cache-Control", "post-check=0, pre-check=0");
XMLHttpReq.setRequestHeader("Pragma", "no-cache");
If it doesn't work, try adding an additional parameter to your url, making it unique and therefore, not caching.
var url="query?option=person&userName="+person.innerHTML + "&d=" + new Date().getTime()
I really don't like this solution, but it helps you to know if the problem is related to cache.

Resources