none
(IE11Preview)cannot read local files with ajax method of JQuery

    Question

  • I'm creating a program to access local files using the ajax method of JQuery.
    This program is working correctly in IE10 and older version.

    But this program shows the message "Error:Access Denied" in IE11Preview.
    I can read with ActiveX or IE10 Compatibility mode.

    I think IE11 limit to access local files with XMLHttpRequest.
    But I could not find the change in Internet Explorer 11 Preview Developer's Guide.
    http://msdn.microsoft.com/en-us/library/ie/bg182636(v=vs.85).aspx

    How can I read local files with ajax ?

    $.ajax({
        url: 'Sample.txt',
        type: 'GET',
        dataType: 'TEXT',
        success: function (data) {
            //something
        },
        error: function (req, text, error) {
            alert(error);
        }
    }); 

    • Edited by AZUMAL Friday, July 05, 2013 7:40 AM
    Friday, July 05, 2013 5:33 AM

Answers

  • Hi,

    I found the cause in jQuery source.

    In IE10 and older version, window.ActiveXObject is true.
    Otherwise in IE11, window.ActiveXObject is false (except "Intranet Zone").

    I want to use window.ActiveXObject("Microsoft.XMLHTTP") in IE11, but
    jQuery.ajaxSettings.xhr method returns window.XMLHttpRequest().

    jQuery-1.7.1.js

    // Functions to create xhrs
    function createStandardXHR() {
        try {
            return new window.XMLHttpRequest();
        } catch( e ) {}
    }
    
    function createActiveXHR() {
        try {
            return new window.ActiveXObject( "Microsoft.XMLHTTP" );
        } catch( e ) {}
    }
    
    // Create the request object
    jQuery.ajaxSettings.xhr = window.ActiveXObject ?
        /* Microsoft failed to properly
         * implement the XMLHttpRequest in IE7 
         * (can't request local files),
         * so we use the ActiveXObject when it is available
         * Additionally XMLHttpRequest can be disabled in 
         * IE7/IE8 so
         * we need a fallback.
         */
    function() {
        return !this.isLocal && 
            createStandardXHR() || createActiveXHR();
        } :
        // For all other browsers, use the standard 
        // XMLHttpRequest object
        createStandardXHR;
    I'm going to create ActiveX object in myself to solve the problem .
    Wednesday, July 10, 2013 5:45 AM

All replies

  • Hi,

    which jQuery version are you using. Please provide a link to your website or mashup that shows the issue when posting to this forum for questions about html, css and scripting for website development.

    see HTML and DOM API change list for IE11

    and/or bugs.jquery.com


    Rob^_^

    Friday, July 05, 2013 6:47 AM
  • Thanks Rob.
    jQuery version is 1.7.
    Program I'm developing is not a web application. It's local html files.

    MyProgram.html

    <script type="text/javascript" src="jquery-1.7.1.js"></script>
    <script type="text/javascript">
    $(document).ready(function () {
    	$.ajax({
        		url: 'Sample.txt',
        		type: 'GET',
        		dataType: 'TEXT',
        		success: function (data) {
       		     //something
        		},
        		error: function (req, text, error) {
        		    alert(error);
        		}
    	}); 	
    });
    </script>

    jquery-1.7.1.js

    if(jQuery.support.ajax){  //Row 8049
        jQuery.ajaxTransport(function(s){
        if(!s.crossDomain || jQuery.support.cors){
            var callback;
            return{
            send: function(headers, complete){
    	 var xhr = s.xhr(),
    	    handle,
    	    i;
    	if(s.username){
    	  xhr.open(s.type, s.url, s.async, s.username, s.password);
    	} else {
                xhr.open(s.typr, s.url, s.async); //Error occurs!
    	}
                  

    Friday, July 05, 2013 7:33 AM
  • Hi AZUMAL,

    Please try to use the latest version of jQuery library.

    <script src="http://code.jquery.com/jquery-latest.min.js"  type="text/javascript"></script>

    Changing jQuery library reference to latest version, the ajax method works in the following sample in IE11.

    http://www.w3schools.com/jquery/tryit.asp?filename=tryjquery_ajax_ajax

    Best regards,


    Yanping Wang
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, July 08, 2013 2:27 AM
    Moderator
  • Thanks Yanping.

    I tried 2.0, 1.10 and 1.9 of jQuery, but the error occurs.The "demo_text.txt" in your sample is a web application resource.But I'd like to read "local text files" from "local html files".

    I saved your sample in my PC(tryjquery_ajax_ajax.htm, jquery.min.js and demo_text.txt) and tried to run the program.But the program is not working.

    Monday, July 08, 2013 5:04 AM
  • Hi,,

    1. try resetting your IE security zones to their recommended defaults.

    check which security zone your 'local' site maps to (File>Properties)....

    the MIME-type for files with a txt extension is text/plain, you have text....

    or

    2.place both domains in your Intranet sites list where XSS filtering is disabled by default.

    Note: there api changes in IE11 limiting use of innerHTML and document.write(ln)....

    You did not say where in your code the 'Access Denied' error is occurring....

    My guess would be that its happening in something.

         success: function (data) {
           
    //something
       
    },


    Rob^_^

    Monday, July 08, 2013 6:48 AM
  • Hi,

    I tried to set security zone of html and text files using alternate data stream.
    The program is not working when I set it to "local" or "Internet",
    but I can read files when it's "Intranet".

    I checked the following

    "Security level for this zone(Internet Zone)"

     Medium-high : not working
     Medium        : not working


    "Security level for this zone(Intranet Zone)"
     high             : not working
     Medium-high : working
     Medium        : working
     Medium-low  : working

    "Enable XSS filter(Internet Zone)"
     Enable : not working
     Disable: not working

    "Enable XSS filter(Intranet Zone)"
     Enable : working
     Disable: working

    I don't understand why it's working.
    I'm investigating the detail.

    • Edited by AZUMAL Monday, July 08, 2013 2:53 PM edit
    Monday, July 08, 2013 2:47 PM
  • Hi,

    I found the cause in jQuery source.

    In IE10 and older version, window.ActiveXObject is true.
    Otherwise in IE11, window.ActiveXObject is false (except "Intranet Zone").

    I want to use window.ActiveXObject("Microsoft.XMLHTTP") in IE11, but
    jQuery.ajaxSettings.xhr method returns window.XMLHttpRequest().

    jQuery-1.7.1.js

    // Functions to create xhrs
    function createStandardXHR() {
        try {
            return new window.XMLHttpRequest();
        } catch( e ) {}
    }
    
    function createActiveXHR() {
        try {
            return new window.ActiveXObject( "Microsoft.XMLHTTP" );
        } catch( e ) {}
    }
    
    // Create the request object
    jQuery.ajaxSettings.xhr = window.ActiveXObject ?
        /* Microsoft failed to properly
         * implement the XMLHttpRequest in IE7 
         * (can't request local files),
         * so we use the ActiveXObject when it is available
         * Additionally XMLHttpRequest can be disabled in 
         * IE7/IE8 so
         * we need a fallback.
         */
    function() {
        return !this.isLocal && 
            createStandardXHR() || createActiveXHR();
        } :
        // For all other browsers, use the standard 
        // XMLHttpRequest object
        createStandardXHR;
    I'm going to create ActiveX object in myself to solve the problem .
    Wednesday, July 10, 2013 5:45 AM
  • Hi,

    thx for the feedback...

    also feature test for ActiveX filtering....

    there's a known issue in IE10, 'My Computer' zone... Internet Options>Advanced tab, check "Enable Active Content to run in files on My Computer"

    Regards.


    Rob^_^

    Wednesday, July 10, 2013 7:27 AM