The function below gets results in ie but not on the other browsers. Any suggestion?
function show_packet(str, company) {
var cam = document.getElementById("company");
if (window.XMLHttpRequest) {
var xmlhttp = new XMLHttpRequest();
} else {
var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("packet_1").innerHTML = xmlhttp.responseText;
document.getElementById("icon_1").innerHTML = "";
}
}
xmlhttp.open("GET", "show_packet.php?car_moto=" + encodeURIComponent(str, true) + "&cam=" + encodeURIComponent(cam.value, true));
xmlhttp.send();
}
vars and functions are hoisted to the top of their containing function. Declare your var once at the top, and only assign it in the if/else.
Related
Follownig is my javascript code, ajax function is used to read response from
server. but xmlhttp.status is always 0 in firefox browser, why? please help me.
function ajaxAsyncRequest() {
var xmlhttp;
if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
} else {
xmlhttp = new ActiveObject("Microsoft.XMLHTTP");
}
//creating asynchrounous GET request
var tempValUrl = $(".urlVal").val();
var urls = tempValUrl + '/Department/departmentAdminTokenReceive';
xmlhttp.open("GET", urls, true);
xmlhttp.send();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4) {
alert("readyState"+xmlhttp.readyState);
alert("status"+xmlhttp.status);
if (xmlhttp.status == 200) {
var data = xmlhttp.responseText;
//for signing
var signarr = data.split("$$");
//for signing
document.getElementById('signName').value = signarr[1];
document.getElementById('signCertName').value = signarr[2];
document.getElementById('signCa').value = signarr[3];
document.getElementById('signExpiryDate').value = signarr[4];
document.getElementById('signPublicKey').value = signarr[5];
}
}
}
}
Trying to bring over data from PHP. I'm using the urls to display images and then I'm using the tags to reorder the array I'm trying to create called data. I'm not sure if I'm parsing correctly.
var data = [];
function importJson(str) {
if (str == "") {
document.getElementById("content").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) {
data = JSON.parse(xmlhttp.response);
alert(xmlhttp.response);
alert(data);
for (var sport_index = 0; sport_index < data.sport.length; sport_index++) {
var url1 = data.sport[sport_index][1];
alert(data.sport);
}
alert(url1);
}
}
xmlhttp.open("GET", "http://server/~name/folder/many.php");
xmlhttp.responseType = "json";
xmlhttp.send();
function buildImage(imagesrc) {
var img = document.createElement('img');
img.src = imagesrc;
document.getElementById('content').appendChild(img);
}
}
xmlhttp.response looks like this
{"sport":[{"ImagesId":"34","ImagesPath":"http:\/\/server\/~name\/folder\/images\/24-08-2014-1408868419.png","Tag":"sport"},{"ImagesId":"30","ImagesPath":"http:\/\/server\/~name\/folder\/images\/23-08-2014-1408824125.png","Tag":"sport"}],"clothes":[{"ImagesId":"33","ImagesPath":"http:\/\/server\/~name\/folder\/images\/23-08-2014-1408824179.png","Tag":"clothes"},{"ImagesId":"32","ImagesPath":"http:\/\/server\/~name\/folder\/images\/23-08-2014-1408824166.png","Tag":"clothes"}],"food":[{"ImagesId":"31","ImagesPath":"http:\/\/server\/~name\/folder\/images\/23-08-2014-1408824158.png","Tag":"food"}]}
But data looks like [object Object] and when I try to use the urls to create images the elements are undefined.
In a Javascript object, you access elements by name, for instance:
data.sport[sport_index]["ImagesPath"]
Or
data.sport[sport_index].ImagesPath
function getOptionsData()
{
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
{
json_options = JSON.parse(xmlhttp.responseText);
}
}
xmlhttp.open("GET", "getData.php", true);
xmlhttp.send();
}
"json_options" is a global variable, which should get filled with the responseText of the XMLHttpRequest, which is containing a valid json String:
[{"id":"3","model":"NZ\/","model1":"","tablenr":"1","tabkey":"SSG\/","length":"4","descript":"Schukostecker gerade","matchcode":"","price":"0","pricex":"0","code":"1","textnr":"0","artikelnr":"0","funktion":"Seite 1"},{"id":"4","model":"NZ\/","model1":"","tablenr":"1","tabkey":"SWS\/","length":"4","descript":"Schuko gewinkelt \/ angled 90 Grad","matchcode":"","price":"0","pricex":"0","code":"1","textnr":"0","artikelnr":"0","funktion":"Seite 1"}]
At this point everything is fine and json_options contains a valid json Object.
The function "getOptionsData" gets called in the function "createOptionsTable":
function createOptionsTable()
{
getOptionsData();
var element = null;
for(var i = 0; i < json_options.length; i++)
{
[...]
When i want to access "json_options" at this point, it says that it is null and i dont have any idea why.
Any help is greatly appreciated, thanks in advance!
In your code the code after getOptionsData(); is executed before the call's success callback. so u get json_options as null
Try like this
function getOptionsData(callback)
{
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
{
json_options = JSON.parse(xmlhttp.responseText);
callback();
}
}
xmlhttp.open("GET", "getData.php", true);
xmlhttp.send();
}
function createOptionsTable()
{
var callback = function()
{
var element = null;
for(var i = 0; i < json_options.length; i++)
{
[...]
}
}
getOptionsData(callback);
}
function getOptionsData()
{
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
{
json_options = JSON.parse(xmlhttp.responseText);
for(var i = 0; i < json_options.length; i++)
{
[...]
}
}
}
xmlhttp.open("GET", "getData.php", true);
xmlhttp.send();
}
Your for loop should execute after you could actually get the reponse from ajax and not inside createOptionsTable()...
Do anyone know what I'm doing wrong or why AJAX callbacks are TOO slow? Here's code:
function new_xmlhttp() {
var xmlhttp;
if(window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
} else {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
return xmlhttp;
}
function ajax_get(data, args) {
xmlhttp = new_xmlhttp();
xmlhttp.open("GET", "functions.php?" + data, true);
xmlhttp.onreadystatechange = function() {
if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
// OK.
alert(args);
}
}
xmlhttp.send(null);
}
Sometimes it takes 2-3 seconds to load (data is max 10 bytes long.)
Tested on Firefox and Chrome under Linux.
My code works in Firefox, but it does not work on the IE
Here is my script:
<script type="text/javascript">
var request = new XMLHttpRequest();
function saseangol() {
request.open ("GET", "saseangol.html",true);
request.onreadystatechange = function() {
if(request.readyState == 4) {
document.getElementById('cont').innerHTML = request.responseText;
}
}
request.send(null);
}
var request = new XMLHttpRequest();
function sase() {
request.open ("GET", "sase.html",true);
request.onreadystatechange = function() {
if(request.readyState == 4) {
document.getElementById('cont').innerHTML = request.responseText;
}
}
request.send(null);
}
</script>
Can someone help me plz?
Can you please check which version of IE you are using, and does it support XMLHttpRequest. Because IE7+ has built-in XMLHttpRequest object.
xmlhttp = new XMLHttpRequest();
Old versions of Internet Explorer (IE5 and IE6) use an ActiveX Object:
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
in javascript:
if (window.XMLHttpRequest) {
self.xmlHttpReq = new XMLHttpRequest();
}
// IE
else if (window.ActiveXObject) {
self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
}
If you want to use AJAX cross-browser you can do something like this...
function createXHR() {
if (typeof XMLHttpRequest !== "undefined") {
return new XMLHttpRequest();
} else {
var versions = ["MSXML2.XmlHttp.6.0",
"MSXML2.XmlHttp.3.0"];
for (var i = 0, length = versions.length; i < length; i++) {
try {
var xhr = new ActiveXObject(versions[i]);
return xhr;
} catch (error) {}
}
}
alert("Your Browser Doesn't Support XmlHttp");
return null;
}
You use asynchronous request so when it should be performed, you just redeclared var request = new XMLHttpRequest(); variable and so there might be reason of your problem