unknown error titanium json using webapp development - javascript

I'm getting an unknown error where the loginConnect.onload should run.
I was following an older tutorial located at http://code.tutsplus.com/tutorials/titanium-user-authentication-part-1--mobile-3728
Any help would be appreciated! thank you!
var loginConnect = Titanium.Network.createHTTPClient();
function gridWindow(e){
if($.username.value != ""&&$.password.value != ""){
loginConnect.open("POST","http://localhost:80/grid/indexgrid.php");
var params = {
username: $.username.value,
password: Ti.Utils.md5HexDigest($.password.value)
};
loginConnect.send(params);
} else{
alert('All fields are required');
}
};
loginConnect.onload = function()
{
var json = this.responseText;
var response = JSON.parse(json);
if (response.logged == true)
{
alert("Welcome back");
} else {
alert('not working');
}
};
$.index.open();

Try this...
loginConnect.send(JSON.stringify(params));

Related

Open new tab onclick JS event using handler ashx

I have a login button with onclick event...onclick = "login();"
I successfully logged in ...but I wanted to open new tab instead.
This is my javascript:
login = function() {
if ($("#UserName").val().length == 0) {
return;
}
if ($("#Password").val().length == 0) {
return;
}
var logindata = new Object();
logindata.UserName = $("#UserName").val();
logindata.Password = $("#Password").val();
locustraxx.showLoading("loginformDiv");
locustraxx.doAjaxPostback('//example.com/LoginHandler.ashx', logindata, null, null,
function(data, textStatus, jqXHR) {
var isChrome = /Chrome/.test(navigator.userAgent) && /Google Inc/.test(navigator.vendor);
if (data.success == true) {
if ($("#Remember").is(':checked')) {
$.cookie('remember', $("#UserName").val() + '|' + $("#Password").val(), {
expires: 36500,
path: '/'
});
}
var retUrl = $.QueryString("ReturnUrl");
if (retUrl == undefined || retUrl == null) {
window.location.replace(data.data, '_blank');
} else {
window.location.href = decodeURIComponent(retUrl).replace(/\~~/g, ".");
}
if (isChrome) {
// clearCookies();
}
return false;
} else {
alert(data.message);
$("#UserName").focus();
}
}, null,
function() {
login.hideLoading("loginformDiv");
});
}
I tried _blank... window open, but to no avail
Could you please specify better what you have tried so far in theory you should be OK with:
window.open('https://www.google.com', '_blank');
If It isn’t working please specify the error output of the console.
There is also a known issue in which the browser opens a new window instead of a new tab, but that has to do with the user preferences, nothing to do about that. See here.

Login using Json

I am trying to develop a login process using json.
My problem is that when I make many login attempts , parameters are not overriden but concatenated.
Below is the code I'm writing.
I do not see where the problem is.
Thank you in advance .
var loginReq = Titanium.Network.createHTTPClient();
loginReq.onload = function() {
var json = this.responseText;
Titanium.API.info("step 1 done");
var response = JSON.parse(json);
if (response.status == true) {
Titanium.App.Properties.setString("key", response.key);
alert("Success");
} else {
alert("Email or password wrong");
}
};
function doConnVerif() {
if ($.email.value != '' && $.password.value != '') {
loginReq.open("POST", "URL");
loginReq.send({
'email' : $.email.value ,
'password' : $.password.value
});
} else {
alert("Enter email and password");
}
}
By your question I can't grasp the full aspect of the problem, but definitely a good idea would be only allow one login attempt at a time.
Something like:
var pendingRequest = false;
// ...
if(!pendingRequest) {
loginReq.send({
'email' : $.email.value ,
'password' : $.password.value
});
pendingRequest = true
}
// ...
loginReq.onload = function() {
pendingRequest = false
// ...

Check if var is set or exists in Angular controller

I have this function in a angular controller:
$scope.sendCompanyData = function() {
delete $scope.company["step1Form"];
delete $scope.company["step2Form"];
delete $scope.standard_address["state"];
$http.post(Routing.generate('create-company'), {
'company': $scope.company,
'standard_address': $scope.standard_address,
'phone': $scope.phone,
'courrier_address': $scope.courrier_address,
'logoFileName': $scope.logoFileName,
'mercantilDocFileName': $scope.mercantilFileName,
'rifDocFileName': $scope.rifFileName,
'standardAddressState': $scope.state.standard_address,
'standardAddressCity': $scope.city.standard_address,
'courrierState': $scope.courrierState.courrier_address,
'courrierCity': $scope.courrierCity.courrier_address
}).success(function(data) {
if (!data.success) {
if (!data.exception) {
$scope.errors = data.errors;
} else {
$scope.errors = data.exception;
}
} else {
$templateCache.removeAll();
ClientUser.loginToCompany(data.companyId);
if ($scope.mercantilFileName != "" && $scope.rifFileName != "") {
$noty.success(Translator.trans('company.register_success'));
} else {
$noty.success(Translator.trans('company.register_document_missing'));
}
$location.path('/empresa/' + data.companyAlias);
}
}).error(function(data, status) {
$scope.error = status;
});
$scope = angular.element($(".seller-layout.new")).scope();
$scope.section = 'segundo-paso';
}
The problem related to this code is on this line $scope.mercantilFileName != "" && $scope.rifFileName != "" since this is not checked or never happen even if I don't send any value on that. I'm getting crazy with this and perhaps it's easy but the code always goes trough else sentence. Any help on this?
Why not simply:
if ($scope.mercantilFileName && $scope.rifFileName) {...}
I think that $scope.mercantilFileNam is rather 'undefined' than an empty string("").

Not able to call a jquery function in a ajax returned code

I am not much of a javascript programmer, I am learning so please forgive the stupid mistakes.
I am trying to save customer details using ajax (its not the jquery ajax), which returns another form for advanced details. I am using a jquery plugin for validation of input boxes. But the validation doesn't work on the form returned by ajax.
Ajax code:
function createcustomer() {
var firstname = _("firstname").value;
var lastname = _("lastname").value;
var email = _("email").value;
var phone = _("phone").value;
var status = _("status");
if (firstname == "" || lastname == "" || email == "" || phone == "") {
status.innerHTML = "Fill out all of the form data";
} else {
_("signupbtn").style.display = "none";
status.innerHTML = 'please wait ...';
var ajax = ajaxObj("POST", "save-customer.php");
ajax.onreadystatechange = function () {
if (ajaxReturn(ajax) == true) {
if (ajax.responseText != "failure") {
_("signupform").innerHTML = "";
_("advancedform").innerHTML = ajax.responseText;
} else {
window.scrollTo(0, 0);
_("signupform").innerHTML = "Error Occured";
}
}
}
ajax.send("firstname=" + firstname + "&lastname=" + lastname + "&email=" + email + "&phone=" + phone);
}
}
function ajaxObj(meth, url) {
var x = new XMLHttpRequest();
x.open(meth, url, true);
x.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
return x;
}
function ajaxReturn(x) {
if (x.readyState == 4 && x.status == 200) {
return true;
}
}
I have tested the ajax return code thouroughly and the jquery works independently on that form.
This is how I initiate the jquery validation plugin :
$(document).ready(function() {
$('form').validationEngine();
});
I tried putting the exact code in the createcustomer() function to reload the jquery function but it doesnt work. Is there any way I can call that jquery validation function in the ajax function?
Any help will be appreciated.
Regards
Priyanshu
I'm suspecting that your validation is trying to find a form that doesnt exists at the execution time, try putting the validation binding when you have sure that the form exists in your DOM. To achieve this you should put the validation binder:
$('form').validationEngine();
...inside the callback after the ajax loads some HTML data in the document. Like this:
ajax.onreadystatechange = function() {
if(ajaxReturn(ajax) == true) {
if(ajax.responseText != "failure"){
_("signupform").innerHTML = "";
_("advancedform").innerHTML = ajax.responseText;
$('#ajaxreturnform').validationEngine('attach'); //Here
} else {
window.scrollTo(0,0);
_("signupform").innerHTML = "Error Occured";
}
}
}
I've looked around here and found this post Attach Validation the accepted answer may inlight your problem also. Try to put the 'attach' command as a parameter to your binder as well as the #id of your form.

jQuery/AJAX: Code works in Chrome but not IE

I'm working on a validation form, I'm sending the data from the html fields with JS pulling the validation with a php document and sending it back via AJAX, it works pretty fine in Chrome but not Internet Explorer (I'm testing it in IE9):
function Checkfiles() {
var fup = document.getElementById('flUpload');
var fileName = fup.value;
var ext = fileName.substring(fileName.lastIndexOf('.') + 1);
var chkext = ext.toLowerCase();
if(chkext=="gif" || chkext=="jpg" || chkext=="jpeg" || chkext=="png") { return true; } else { return false; }
} // Checkfiles
function Checksize() {
var iSize;
if ($("#flUpload")[0].files[0]){ iSize = ($("#flUpload")[0].files[0].size / 1024);}
if(Checkfiles()==true && iSize <= 51.200) { return true; } else { return false; }
} //Checksize
$(document).ready(function() {
$("#ff1").submit(function(e){
// prevent submit
e.preventDefault();
var email = document.getElementById("email").value;
var title = document.getElementById("title").value;
var url = document.getElementById("url").value;
var parametros = {"emaail":email, "tiitle":title, "uurl":url};
$.ajax({
data: parametros,
url: 'validate.php',
type: 'post',
context: this,
error: function (response) {
alert("An error has occurred! Try Again!");
},
success: function (response) {
if($.trim(response)=='b' && Checksize()==true) {
this.submit(); // submit, bypassing jquery bound event
}
else {
if (Checksize()==true) {
$("#ajax_call_val").html('<div id="validation"><ul>'+response+'</ul></div>');
} else {
if(response!='b') {
$("#ajax_call_val").html('<div id="validation"><ul>'+response+'<li>Only GIF, PNG, JPG images, smaller than 50 KB</li></ul></div>');
} else { $("#ajax_call_val").html('<div id="validation"><ul><li>Only GIF, PNG, JPG images, smaller than 50 KB</li></ul></div>'); }
}
}
}
});
});
});
Any wonder why is not working in IE? Thanks in advance.

Categories

Resources