Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * @method prepareRequest
- * @param {object} e Callback event
- */
- var window = Ti.UI.createWindow();
- var button1 = Ti.UI.createButton({title:"Basic request", top: 30});
- var button2 = Ti.UI.createButton({title:"Auth request", top: 90});
- window.add(button1);
- window.add(button2);
- button1.addEventListener('click', prepareRequest);
- button2.addEventListener('click', prepareRequest);
- function prepareRequest(e) {
- var withCredentials = true;
- request(withCredentials);
- }
- /**
- * Make a request
- * @method request
- * @param {boolean} withCredentials Flag for make a auth request or not
- */
- function request(withCredentials) {
- var client,
- config,
- url,
- data;
- data = {
- message: 'This is my message'
- };
- config = {
- onload: success,
- onerror: error,
- timeout: 5000
- };
- if (withCredentials) {
- url = 'https://srvasic.upv.es/recursos/error401.asp';
- //config.withCredentials = true;
- config.username = 'user';
- config.password = 'p$wd';
- } else {
- url = 'https://httpbin.org/post';
- }
- console.log('HTTP CLIENT CONFIG ' + JSON.stringify(config));
- client = Ti.Network.createHTTPClient(config);
- client.setOnload(success);
- client.setOnerror(error);
- try {
- console.log('URL: ' + url);
- client.open('POST', url);
- } catch (e) {
- console.log('error opening client');
- console.log(JSON.stringify(e));
- }
- try {
- client.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
- } catch (e) {
- console.log('error setting request headers');
- console.log(JSON.stringify(e));
- }
- try {
- client.send(data);
- } catch (e) {
- console.log('error sending data');
- console.log(JSON.stringify(e));
- }
- }
- /**
- * HttpClient Success Callback
- * @method success
- * @param {object} e Callback Event
- */
- function success(e) {
- console.log('Success callback: ' + this.responseText);
- }
- /**
- * HttpClient Error Callback
- * @method error
- * @param {object} e Callback Event
- */
- function error(e) {
- console.log('Error callback : ' + e.error);
- }
- window.open();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement