×

Discussion Board

Results 1 to 7 of 7
  1. #1
    Registered User
    Join Date
    Apr 2013
    Posts
    12

    How to make ajax calls in s40 web apps?

    Hi,

    I want to make an ajax call to retrieve the contents of a text file located on a different server using pure javascript only (without the use of jquery etc.). How should I go about doing this? I have tried the following but it does not work. I am getting an empty string in the responseText property and an error saying - Error: INVALID_STATE_ERR: DOM Exception 11 in the status property. Any help would be grateful.

    Code:
    var xmlhttp=new XMLHttpRequest();
    
    	xmlhttp.open("GET", "http://mydomain.com/file_name.txt", true);
    	xmlhttp.onreadystatechange = function(){
    	if(xmlhttp.readyState==4 && xmlhttp.status==200){
    		var response = xmlhttp.responseText;
    	}
    	xmlhttp.send();

  2. #2
    Nokia Developer Moderator
    Join Date
    May 2007
    Location
    21.46 N 72.11 E
    Posts
    3,796

    Re: How to make ajax calls in s40 web apps?

    Try this,

    Code:
    var ajaxRequest = {
    		
    	URL: "http://www....",
    
    	init: function(){
    		//URL, success callback, failure callback		
    		this.connect(this.URL, this.responseHandler, this.failureHandler);
    	},
    
    	responseHandler: function(data) {
    		// Process data
    	},
    
    	failureHandler: function(reason){	
                    // Handle failure
    	}, 	
    	  
    	connect: function(url, successCb, failCb) {
    		
    		var xmlhttp = new XMLHttpRequest();
    		
    		xmlhttp.open("GET", url, true);
    
    		xmlhttp.setRequestHeader("Accept","application/json");	
    		xmlhttp.setRequestHeader("Cache-Control", "no-cache");
    		xmlhttp.setRequestHeader("Pragma", "no-cache");
    		
    		var that = this;
    		xmlhttp.onreadystatechange= function() {
    			
    			if (xmlhttp.readyState ==  4 ){
    				
    				if(xmlhttp.status == 200){		
    	                if (xmlhttp.responseText != null) {
    	                	successCb.call(that, xmlhttp.responseText);
    					}else{
    						failCb.call(that, "Empty response.");
    					}	
    				}else{				
    					failCb.call(that, "Connection failed: Status "+xmlhttp.status);
    				}
    			}
    		};
    		xmlhttp.send();
    	}		
    };
    
    ajaxRequest.init();
    Pankaj Nathani
    www.croozeus.com

  3. #3
    Registered User
    Join Date
    Apr 2013
    Posts
    12

    Re: How to make ajax calls in s40 web apps?

    Its not working.. I am getting the following errors:
    OPTIONS http://mydomain.com/file_name.txt 501 (Not Implemented)
    XMLHttpRequest cannot load http://mydomain.com/file_name.txt. Origin null is not allowed by Access-Control-Allow-Origin.

    What could be the possible reason for this?

  4. #4
    Nokia Developer Expert
    Join Date
    Jun 2008
    Location
    Tampere
    Posts
    833

    Re: How to make ajax calls in s40 web apps?

    Hi,

    Are you seeing these errors in Web Tools local preview or somewhere else?

    -Ilkka

  5. #5
    Nokia Developer Moderator
    Join Date
    May 2007
    Location
    21.46 N 72.11 E
    Posts
    3,796

    Re: How to make ajax calls in s40 web apps?

    Quote Originally Posted by anujit View Post
    Its not working.. I am getting the following errors:
    OPTIONS http://mydomain.com/file_name.txt 501 (Not Implemented)
    XMLHttpRequest cannot load http://mydomain.com/file_name.txt. Origin null is not allowed by Access-Control-Allow-Origin.

    What could be the possible reason for this?
    It seems to me that you're trying to load a file (.txt) with that URL. Don't you have a web service running on your server which can give you a response instead of trying to load data from a txt file?
    Pankaj Nathani
    www.croozeus.com

  6. #6
    Registered User
    Join Date
    Apr 2013
    Posts
    12

    Re: How to make ajax calls in s40 web apps?

    i ran the code in the web tools local simulator just now.. the errors are not seen.. i was running in chrome until now and was seeing the errors..

  7. #7
    Registered User
    Join Date
    Apr 2013
    Posts
    12

    Re: How to make ajax calls in s40 web apps?

    thanx croozeus.. ur code is working fine in the web tools local simulator..

Similar Threads

  1. Replies: 2
    Last Post: 2011-04-13, 20:05
  2. Ajax calls failing (WRT, Prototype, 5800)
    By massive-pebkac in forum Symbian
    Replies: 3
    Last Post: 2010-10-06, 14:16
  3. Replies: 2
    Last Post: 2010-08-29, 06:28
  4. AJAX Remote calls?
    By aiveldesign in forum Symbian
    Replies: 8
    Last Post: 2010-06-20, 01:07
  5. Aborting Ajax calls using XMLHttpRequest.abort();
    By JasperGoes in forum Symbian
    Replies: 2
    Last Post: 2010-04-19, 12:47

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×