×

Discussion Board

Results 1 to 2 of 2
  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    83

    Json post example for nokia s40 web tools 1.5

    Any one please send me an example for nokia web tools 1.5 (s40) which post http request with parameter and get the json response, because my javascript code is running fine on symbian device but not on s40 devices.

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

    Re: Json post example for nokia s40 web tools 1.5

    Hi,

    It should work ok. Have you checked if you do get a response from the service or is the problem in displaying the results in Web apps side?

    Anyhow here is some code that you could play with:
    Code:
    function init(){			
    	connect("http://yourserver-example.ext/echo.php", responseHandler, failureHandler);
    }
    
    function responseHandler(json) {	
    	//For commercial applications usage of json2.js is recommended, instead of eval.
    	//see https://github.com/douglascrockford/JSON-js	
    	var obj = eval('(' + json + ')');		
    	document.body.innerHTML += obj.param1 +" "+obj.param2;
    }
    
    function failureHandler(reason) {	
    	document.body.innerHTML = "Could not get JSON data.<br>"+ reason;
    }
    
    function connect(url, successCb, failCb) {
    	
    	var xmlhttp = new XMLHttpRequest();
    	
    	var params = "param1=test&param2=test2";
    	
    	xmlhttp.open("POST", url, true);
    
    	xmlhttp.setRequestHeader("Accept","application/json");
    	xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    	xmlhttp.setRequestHeader("Content-length", params.length);
    	xmlhttp.setRequestHeader("Connection", "close");
    	
    	xmlhttp.onreadystatechange= function() {
    		
    		if (xmlhttp.readyState ==  4 ){
    			
    			if(xmlhttp.status == 200){		
                    if (xmlhttp.responseText != null) {
                    	successCb(xmlhttp.responseText);
    				}else{
    					failCb("Empty response.");
    				}	
    			}else{				
    				failCb("Connection failed: Status "+xmlhttp.status);
    			}
    		}
    	};
    	xmlhttp.send(params);
    }

    echo.php
    Code:
    header("Content-Type: application/json");
    $param1=$_POST["param1"];
    $param2=$_POST["param2"];
    
    echo '{ "param1": "'.$param1.'", "param2": "'.$param2.'" }';
    Br,
    Ilkka

Similar Threads

  1. Replies: 5
    Last Post: 2012-03-13, 13:41
  2. Nokia wrt ajax JSON post
    By SHSaqlaini in forum General Development Questions
    Replies: 9
    Last Post: 2011-11-18, 10:13
  3. Replies: 1
    Last Post: 2011-10-17, 08:05
  4. How to download JSON ME Api "org.json.me.zip"
    By narendar_discover in forum Mobile Java Networking & Messaging & Security
    Replies: 5
    Last Post: 2010-05-01, 12:15

Posting Permissions

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