I'm parsing remote data in to my app and uses it through arguments. One of the data types is a url adresse i want to open in the url. I have an idea that I have to open it with the openURL function but I can't seem to get it to work. Anyone have a working example?
You have to utilize in-built HttpClient
var url = "http://www.you_remote_url.com";
var client = Ti.Network.createHTTPClient({
// function called when the response data is available
onload : function(e) {
Ti.API.info("Received text: " + this.responseText);
alert('success');
},
// function called when an error occurs, including a timeout
onerror : function(e) {
Ti.API.debug(e.error);
alert('error');
},
timeout : 5000 // in milliseconds
});
// Prepare the connection.
client.open("GET", url);
// Send the request.
client.send();
Related
I'm using Http from #angular/http to send GET requests, but the server is not receiving the request. The generated urls are correct because when I log them and open them in browser (I've tried all of Chrome, Firefox and Safari), the server does receive these requests.
This is how I am doing this:
let logButtonUrl = this.urlGenerator.generateTiramisuUrlTemp(this.servletPath,
argMap);
console.log("logButtonUrl:"+logButtonUrl);
return this.http.get(logButtonUrl).map(this.writeSuccess);
Function writeSuccess:
private writeSuccess(res: Response) {
let body = res.json();
let rows_affected = body.data[0].rowsAffected;
if (rows_affected == "1") {
return true;
} else {
return false;
}
}
I got no error message in browser console, so it's probably not because of the CORS issue discussed here:
http://blog.ionic.io/handling-cors-issues-in-ionic/
I also tried using a proxy. I added this in ionic.config.json:
{
"path": "/backendTemp",
proxyUrl": "http://128.237.217.70:8080" /*the ip address of the target server*/
}
And replace the ip address in my generated urls with "/backendTemp". Still not working.
Any suggestions/thoughts on this? Thanks a lot!
Use the $http (https://docs.angularjs.org/api/ng/service/$http):
.controller('RequestCtrl', function ($http) {
$http({
method: 'GET',
url: 'http://128.237.217.70:8080/backendTemp'
}).then(function successCallback(response) {
// this callback will be called asynchronously
// when the response is available
}, function errorCallback(response) {
// called asynchronously if an error occurs
// or server returns response with an error status.
});
Is there any way that i can set request headers in titanium?.
eg: API = abcdefgb56432142345234534;
my xhr:
var url = "http://www.appcelerator.com";
var client = Ti.Network.createHTTPClient({
// function called when the response data is available
onload: function(e) {
Ti.API.info("Received text: " + this.responseText);
alert('success');
},
// function called when an error occurs, including a timeout
onerror: function(e) {
Ti.API.debug(e.error);
alert('error');
},
timeout: 5000 // in milliseconds
});
// Prepare the connection.
client.open("GET", url);
// Send the request.
client.send();
Yes try the below,
Always set after opening the request
var url = "http://www.appcelerator.com";
var client = Ti.Network.createHTTPClient({
// function called when the response data is available
onload: function(e) {
Ti.API.info("Received text: " + this.responseText);
alert('success');
},
// function called when an error occurs, including a timeout
onerror: function(e) {
Ti.API.debug(e.error);
alert('error');
},
timeout: 5000 // in milliseconds
});
// Prepare the connection.
client.open("GET", url);
client.setRequestHeader('API','abcdefgb56432142345234534'); //allways set after open
// Send the request.
client.send();
try{
var targetURL ="http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Specialty/ESRI_StateCityHighway_USA/MapServer/1/query?where=STATE_NAME%3D%27Florida%27&f=json";
var xhrArgs = {
url: targetURL,
handleAs: "json",
load: function(data) {
alert(data);
},
error: function(error) {
alert("An unexpected error occurred: " + error);
}
};
var deferred = dojo.xhrGet(xhrArgs);
}catch(e){
alert(e);
}
Is this the right way of calling rest service?
I am getting null response from the above code.
dojo/xhr cannot be used for cross domain requests.
Is http://sampleserver1.arcgisonline.com/ the same domain that loads the web page or a different server?
If it is the same server, drop the domain name (ie ArcGIS/rest/services/...);
If not, you can use jsonp
https://dojotoolkit.org/reference-guide/1.9/dojo/request/script.html#dojo-request-script
Another alternative is to call a service located on the web server that acts as a proxy and makes the call to the other server.
I have some JSONP in my application, & I want to set timeout for them. How can I set it?
it maybe something like this, if it is possible :)
Ext.util.JSONP.request({
url: mhid.dashboard.hbf.controller+'/get_dashboard'
,method: 'POST'
,timeout : 50000 // TIMEOUT
,callbackKey: 'jsonp_callback'
,params:{
'site' : site
,'fleet' : fleet
,'sn' : sn
,'format' : 'json'
}
,callback: function(response, opts) {
var obj = response;
tpl.overwrite('content', obj);
loadMask.hide();
}
,failure: function(response, opts) {
alert('Failure');
}
});
Thanks in advance
I don't think it is possible using JSONP - you should take a look at the Ext.Ajax class. Also, POST is not possible using JSONP (refer to the Sencha forum
You will need to implement your own timeout and execute the failure callback:
var failureCallback = function() {
// MUST remove the current request,
// or it will not dispatch any subsequent new JSONP request
// unless you refresh your page
delete Ext.util.JSONP.current;
alert('Failure');
};
Ext.util.JSONP.request({..}); // as usual
var failureTimeoutId = setTimeout(function() {
failureCallback();
}, 30000);
discard the timeout if your request is success
discard current request if the request has timed out
Does anyone have an example of an API response being passed back from a http.request() made to a 3rd party back to my clientSever and written out to a clients browser?
I keep getting stuck in what I'm sure is simple logic. I'm using express from reading the docs it doesn't seem to supply an abstraction for this.
Thanks
Note that the answer here is a little out of date-- You'll get a deprecated warning. The 2013 equivalent might be:
app.get('/log/goal', function(req, res){
var options = {
host : 'www.example.com',
path : '/api/action/param1/value1/param2/value2',
port : 80,
method : 'GET'
}
var request = http.request(options, function(response){
var body = ""
response.on('data', function(data) {
body += data;
});
response.on('end', function() {
res.send(JSON.parse(body));
});
});
request.on('error', function(e) {
console.log('Problem with request: ' + e.message);
});
request.end();
});
I would also recommend the request module if you're going to be writing a lot of these. It'll save you a lot of keystrokes in the long run!
Here is a quick example of accessing an external API in an express get function:
app.get('/log/goal', function(req, res){
//Setup your client
var client = http.createClient(80, 'http://[put the base url to the api here]');
//Setup the request by passing the parameters in the URL (REST API)
var request = client.request('GET', '/api/action/param1/value1/param2/value2', {"host":"[put base url here again]"});
request.addListener("response", function(response) { //Add listener to watch for the response
var body = "";
response.addListener("data", function(data) { //Add listener for the actual data
body += data; //Append all data coming from api to the body variable
});
response.addListener("end", function() { //When the response ends, do what you will with the data
var response = JSON.parse(body); //In this example, I am parsing a JSON response
});
});
request.end();
res.send(response); //Print the response to the screen
});
Hope that helps!
This example looks pretty similar to what you are trying to achieve (pure Node.js, no express):
http://blog.tredix.com/2011/03/partly-cloudy-nodejs-and-ifs.html
HTH