Software Archive
Read-only legacy content
17061 Discussions

Error with XMLHttpRequest () in IOS app.

pedro_v_
Beginner
1,869 Views

I developed a HTML5 app in XDK reading from a local XML file. In the emulator it works properly, and built on android app as well, but the test does not work ios.
access to the XML file is done with a call:
Connect var = new XMLHttpRequest ();
Connect.open ("GET", "data /archivename.xml file", false);
Connect.setRequestHeader ("Content-Type", "text / xml");
Connect.send (null);

What is the problem?

0 Kudos
8 Replies
Amrita_C_Intel
Employee
1,869 Views

Hello,

Under the "Build Settings" you will find a section named "Domain List" where you can enter the domains you wish to provide access to (or use * or all domains). Can you check that?

Which XDK version are you using?

0 Kudos
Swati_S_Intel1
Employee
1,869 Views

@pedrovi, On the device the relative path will not work. You have to get the path of the folder where you app resides. To get the webroot use this function and then attache your relative path to it to access the file.

function getWebRoot() {
    "use strict" ;
    var path = window.location.href ;
    path = path.substring( 0, path.lastIndexOf('/') ) ;
    return path ;
}

 

0 Kudos
pedro_v_
Beginner
1,869 Views

I have changed the relative path with your function, but the result is the same. In the emulate works fine, the same in the debug on android device (intel preview), but in the ios app preview doesn´t work. This is the code:

function getWebRoot() {
                            "use strict" ;
                            var path = window.location.href ;
                            path = path.substring( 0, path.lastIndexOf('/') ) ;
                            return path ;
                        }

                            //Read xml file    
                                var Connect = new XMLHttpRequest();
                                var FileName = getWebRoot() + "/file.xml";
                                Connect.open("GET", FileName, false);

 

I have Intel XDK 2496 version on Mac

0 Kudos
Anusha_M_Intel1
Employee
1,869 Views

There have been users who have faced this issue trying to access media files on device. Here is a detailed answer to locating the application's root directory on different platforms: http://stackoverflow.com/questions/21321335/intel-xdk-directory-browsing/21392617#21392617

Users have found this method useful. If it doesn't solve your issue, please post back. Thanks!

 

0 Kudos
son_n_
Beginner
1,869 Views

There have been users who have faced this issue trying to access media files on device. Here is a detailed answer to locating the application's root directory on different platforms: http://stackoverflow.com/questions/21321335/intel-xdk-directory-browsing...

Users have found this method useful. If it doesn't solve your issue, please post back. Thanks!

I have same problem and it doesn's solve this issue. Can you give us some advice?

My config:

http://pictub.club/image/V2clY

My code:

function getWebRoot() {
    "use strict" ;
    var path = window.location.href ;
    path = path.substring( 0, path.lastIndexOf('/') ) ;
    return path ;
}
function loadText(url,callback) {
		alert(getWebRoot()+"/" +url);
		var xhr = new XMLHttpRequest();

		xhr.responseType = 'text';
		xhr.onreadystatechange = function() { 
		  if (xhr.readyState == 4) {  //Request is complete
			if(xhr.state == 200) {  //Response is ready
			  alert("Request succeeded. Data:  "+xhr.responseText);
callback(null, xhr.response);
			} else { //Error handler
			  alert('Request failed with error code: ' + xhr.state); 
callback(xhr.response, null);
			}
		  }
		}

		xhr.open("GET", getWebRoot() +"/"+url,true);
		xhr.send(null);
}

I got error 'Request failed with error code: undefined'...

0 Kudos
PaulF_IntelCorp
Employee
1,869 Views

See this FAQ > https://software.intel.com/en-us/xdk/faqs/app-designer#ajax-jquery-one-fail < it might have some clues. You may also need to check for a return code of 0, not just 200.

0 Kudos
son_n_
Beginner
1,869 Views

Hello,

I fix this issue by change the extension .json to .txt.

Thanks

0 Kudos
son_n_
Beginner
1,869 Views

Paul F. (Intel) wrote:

See this FAQ > https://software.intel.com/en-us/xdk/faqs/app-designer#ajax-jquery-one-fail < it might have some clues. You may also need to check for a return code of 0, not just 200.

I think it's a bug on iOS. If we request to a json file first, the request always fail with undefined code return.

Note: After I change the extension of the first url request from json to txt and it work; all latter request work with json url.

Sorry for my bad English

Thanks

0 Kudos
Reply