1.11M Members

Ajax not loading local XML file

 
0
 

Hi,
I have set of html pages that are installed locally on machine.
I want to make ajax call to local files.
In Firefox everything works fine.
IE is able to load html pages through ajax call but not able to load xml document.

Chrome doesn't support ajax call locally...and I don't care about it. I need it to work on IE & firefox.

Can anyone help me out.

Thanks

 
0
 

a. Post the code you have thus far (the file doing the "importing")
b.specify how you are accessing/loading your test page from the browser (ex: using http://localhost, file:///path/to/file, http://yoursite.com)
c. post a sample of the xml file you are trying to load.

 
0
 

a) Post the code you have thus far (the file doing the "importing")
b) specify how you are accessing/loading your test page from the browser (ex: using http://localhost , file:///path/to/file, http://yoursite.com )
c) post a sample of the xml file you are trying to load.

A) This is code that I am using to load filters.xml because ajax Call is not fetching xml file

if( window.ActiveXObject && /Win/.test(navigator.userAgent) )
   {
        try{
            xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
            xmlDoc.async="false";
            xmlDoc.loadXML("filters.xml");
            alert(xmlDoc.xml);
        }catch(ex){
            alert("exception:"+ex);
        }


}

B) I am loading my xml page into browser with file://c:/cbd/... (file protocol)
C) Below is the sample xml file

<?xml version="1.0" standalone="yes"?>
<filters>
    <runtimes>
        <name>Runtimes</name>
        <runtime name="air" default="2">

        </runtime>

    </runtimes>

    <products>
        <name>Products</name>
     </products>
</filters>
 
0
 

I have tried load function xmlDoc.Load("filters.xml") but it throws exception.

 
0
 

loadXML() is for inline xml, not for a file. To load a file you need the load() method. Also, make sure the xml file is in the same folder as the page where you have the test page OR provide a full path to your xml file.

Don't forget to escape backslashes:
WRONG: var file="C:\somefolder\file.xml"; CORRECT: var file="C:\\somefolder\\file.xml";

 
0
 

loadXML() is for inline xml, not for a file. To load a file you need the load() method. Also, make sure the xml file is in the same folder as the page where you have the test page OR provide a full path to your xml file.

Don't forget to escape backslashes:
WRONG: var file="C:\somefolder\file.xml"; CORRECT: var file="C:\\somefolder\\file.xml";

a. xml file is in the same folder as the current page.
b. I had used load() function but it is throwing an exception when I am trying to load xml.

I don't know what else needs to be done. I am stuck on this for 2 days.

 
0
 

The security settings on your IE might be preventing your script from working. Go to IE and click on:

Tools > Internet Options > Security > Local Intranet > Custom Level > Settings
> ActiveX Controls and plugins > Initialize and script ActiveX controls not marked as safe for scripting =>Enable

On the script you posted above, change loadXML() to load().

Then try it again.

 
0
 

The security settings on your IE might be preventing your script from working. Go to IE and click on:

Tools > Internet Options > Security > Local Intranet > Custom Level > Settings
> ActiveX Controls and plugins > Initialize and script ActiveX controls not marked as safe for scripting =>Enable

On the script you posted above, change loadXML() to load().

Then try it again.

Thanks hielo...it does work that way but may application will be used by many people and I cannot ask them to change this setting. Isn't there some other way.

In IE, ajax call made to html page works fine but for xml file it is creating problems.
How does it happen that xmlhttpRequest happen for html page but not xml page.

 
0
 

You must be doing something wrong. I tried using the ajax approach and it works fine.
You are probably trying to get the responseXML from your ajax object, but there is no SERVER involved that "tells" the browser that the content IS xml. This is a well known issue in IE. Instead, you need to get the responseText from it, which should give you the xml as a string. Then use that string with the code you posted above(http://www.daniweb.com/forums/post1327817.html#post1327817), but instead of xmlDoc.loadXML("filters.xml") , you will need xmlDoc.loadXML(http.responseText) NOTE: http a reference to the ajax object you will be creating.

 
0
 

You must be doing something wrong. I tried using the ajax approach and it works fine.
You are probably trying to get the responseXML from your ajax object, but there is no SERVER involved that "tells" the browser that the content IS xml. This is a well known issue in IE. Instead, you need to get the responseText from it, which should give you the xml as a string. Then use that string with the code you posted above(http://www.daniweb.com/forums/post1327817.html#post1327817), but instead of xmlDoc.loadXML("filters.xml") , you will need xmlDoc.loadXML(http.responseText) NOTE: http a reference to the ajax object you will be creating.

I invested a bit further and found that Open method is throwing "permission denied" exception.

page_request.open('GET', url, true);
 
0
 

that typically happens when you are trying to do CROSS domain request or CROSS protocol requests.

 
0
 

that typically happens when you are trying to do CROSS domain request or CROSS protocol requests.

My html files and javascript files resides on my local machine. And I am trying to make a call to my local xml file. If CROSS domain/protocol been the case then I shouldn't be getting the html files by XMLHttpRequest but I am able to get html files.

 
0
 

My html files and javascript files resides on my local machine. And I am trying to make a call to my local xml file. If CROSS domain/protocol been the case then I shouldn't be getting the html files by XMLHttpRequest but I am able to get html files.

Below is my code to make an ajax call.

function ajaxGet(url, responseHandler)
{
	var page_request = false;

	if (window.XMLHttpRequest && !(window.ActiveXObject && window.location.protocol == "file:")) { 
                // use this only if available, and not using IE on a local filesystem
		page_request = new XMLHttpRequest();
        }
	else if (window.ActiveXObject) { // older versions of IE, or IE on a local filesystem
		try {
		      
			page_request = new ActiveXObject("Msxml2.XMLHTTP");
			//page_request = new ActiveXObject("Microsoft.XMLDOM");
			
		} 
		catch (e){
			try{
			     
				page_request = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e){
			 alert("exception:"+e);
			}
		}
	}
	else {
		alert("Your browser does not support XMLHTTP.");
		return false;
	}

	
	page_request.onreadystatechange=function() {
        
		if(page_request.readyState==4) {
                        // on local machines the status for success is 0. on web servers it is 200
			if(page_request.status==200 || page_request.status==0) {
		          
				responseHandler(page_request);
			}
		}
	}
	try{
    	page_request.open('GET', url, true);
    	page_request.send(null);
	}catch(ex){
	   alert("ex:"+ex.message);
	}
}
 
0
 

Thanks Hielo for your help.

I was able to load and parse xml with the below code. But now the only problem is that I have to change the security setting as suggested by you above
("initialize and script ActiveX controls not marked as safe for scripting =>Enable").
I don't want to do this. Is there a workaround for this.

var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
        
		try{
			 xmlDoc.async="true"; 
			 //xmlDoc.onreadystatechange=verify; 
			 xmlDoc.load("filters.xml"); 
			 
		}catch(ex){
		  alert("exception");
		  alert(ex.message);
		  
        }
 
0
 

You must be doing something wrong. I tried using the ajax approach and it works fine.
You are probably trying to get the responseXML from your ajax object, but there is no SERVER involved that "tells" the browser that the content IS xml. This is a well known issue in IE. Instead, you need to get the responseText from it, which should give you the xml as a string. Then use that string with the code you posted above(http://www.daniweb.com/forums/post1327817.html#post1327817), but instead of xmlDoc.loadXML("filters.xml") , you will need xmlDoc.loadXML(http.responseText) NOTE: http a reference to the ajax object you will be creating.

Can you post me the example for this ajax approach to get the xml?
Thank you

 
0
 

Let's take it one step at a time. Originally you said:

In Firefox everything works fine.
IE is able to load html pages through ajax call but not able to load xml document.

If upon completion of the ajax request you put: alert(page_request.responseText) a. Do you see the xml string in FF?
b. Do you see the xml string in IE?

If you answered yes to both of them, then try the following changes:

...
if(page_request.status==200 || page_request.status==0) {
	if(window.ActiveXObject && page_request.status==0)
	{
		page_request.responseXML.loadXML(page_request.restponseText);
	}         
	responseHandler(page_request);
}
...

If you answered yes to "a", and no to "b" then your permissions are definitely to restrictive and you will have to ask the users to loosen it. They don't have to loosen the restrictions on the "Internet" zone, but they can do so in the "Local Intranet" zone.

 
0
 

Let's take it one step at a time. Originally you said:


If upon completion of the ajax request you put: alert(page_request.responseText) a. Do you see the xml string in FF?
b. Do you see the xml string in IE?

If you answered yes to both of them, then try the following changes:

...
if(page_request.status==200 || page_request.status==0) {
	if(window.ActiveXObject && page_request.status==0)
	{
		page_request.responseXML.loadXML(page_request.restponseText);
	}         
	responseHandler(page_request);
}
...

If you answered yes to "a", and no to "b" then your permissions are definitely to restrictive and you will have to ask the users to loosen it. They don't have to loosen the restrictions on the "Internet" zone, but they can do so in the "Local Intranet" zone.

a.) Yes
b.) No

Now I am trying a different approach, using XML data island.

 
0
 

OK so you are still NOT able to see anything via alert(page_request.responseText) when requesting an XML file. What about if you try an html file. It seems like originally you were able to do so. I've never come across an IE version where you can get the HTML contents but not the XML contents (verified via the responseText property of the ajax object).

 
0
 

I know you changed the permissions as I previously suggested just to see if what you had then worked fine (which it did), but I also know you then restored your permissions. Are you absolutely sure you restored your permissions correctly? Based on your original post, where you said "IE is able to load html pages through ajax call but not able to load xml document." It sounds like originally you were able to get the content via responseText, but now you seem to be getting permission denied. Does IE even prompt you if you want to run the 'active content' (the yellow bar that pops up at the top requiring you to click on it and decide whether allow the script to run)?

 
0
 

OK so you are still NOT able to see anything via alert(page_request.responseText) when requesting an XML file. What about if you try an html file. It seems like originally you were able to do so. I've never come across an IE version where you can get the HTML contents but not the XML contents (verified via the responseText property of the ajax object).

Yes, I am still able to get html file through ajax call but not xml file.
page_request.open('GET','filters.xml',true) method throws Access denied exception for xml file only. So I never get a chance to test the responseText property for xml response.

You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: