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.
Printable View
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.
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¶m2=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);
}
[/code]
echo.php
[code]
header("Content-Type: application/json");
$param1=$_POST["param1"];
$param2=$_POST["param2"];
echo '{ "param1": "'.$param1.'", "param2": "'.$param2.'" }';
[/code]
Br,
Ilkka