Using the function storeSessionId() , I retrieved the value of storedSession and assigned to sessionId from the servlet using AJAX.
var storedSession;
var sessionId;
function storeSessionId(){
try
{
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
storedSession = xmlhttp.responseText.toString();
sessionIdValue(storedSession);
}
};
xmlhttp.open("POST","JoinAction?subject="+subject+"&userId="+userId+"&secureKey="+secureKey,true);
xmlhttp.send();
}
catch(err)
{
alert(err.description);
}
}
When I compared the sessionId!=null only my div will be displayed else it cannot be displayed.
But even if I got sessionId==null also, my div was displayed. My div was displayed in both the condition.
function sessionIdValue(storedSession){
sessionId = storedSession;
if(sessionId != null && sessionId != "null"){
document.getElementById("div").style.display="inline";
}
}
Most likely sessionId is an empty string "". Try changing the condition to
if(sessionId && sessionId != "null")
This will just check for a truthy value which the empty string is not.
You are comparing sessionId != null which is incorrect.
Put alert(sessionId) to know the response value.
Try:
if(sessionId && sessionId != "null"){
document.getElementById("div").style.display="inline";
}
First condition checks whether sessionId is not undefined and then for null string.
The solution is ,
function trim(stringToTrim) {
return stringToTrim.replace(/^\s+|\s+$/g, "");
}
I passed the sessionId value in to trim()...
Thanks for all who helped me..
Related
In my console log the array look like this:
{"userid":"5502","fullname":"My fullname","email":"sample#yahoo.com","user_access":"real"}
Now on my ajax, I have a handle code for the data array that the server sends to the app:
function handleData(responseData) {
var access = responseData;
console.log(access);
if (access == '"real"') {
alert("Welcome");
location.href = "home.html";
} else {
alert("Your username and password didn\'t match.");
}
}
How I can get the specific value of this "user_access":"real" in array and use it in condition.
like this:
if (access == '"real"') { // What should be the format of access variable?
alert("Welcome");
location.href = "home.html";
}
function handleData(responseData) {
var response = JSON.parse(responseData);//assuming you are getting the response as a string
var access = response.user_access;
console.log(access);
if (access == "real") {
alert("Welcome");
location.href = "home.html";
} else {
alert("Your username and password didn\'t match.");
}
}//handleData()
Normally, we want our response to be in json ( or we can say 'object' ) form, so that we can easily access its inner properties. So, if it's already an object, you do not need to use JSON.parse. You can directly access any property like this - responseData.user_access . But if it's in string form, then you have to use JSON.parse() first to parse the string into JSON ( or object ) format.
If it is not surrounded by "" around the {} brackets then just do
function handleData(responseData) {
var access = responseData.access;
if (access === 'real') {
alert("Welcome");
location.href = "home.html";
} else {
alert("Your username and password didn\'t match.");
}
}
Have a weird problem in javascript. I am performing a check against the backend with Jquery to check if the mail is already existing in the database. The answer is either true or false.
The problem is that if the answer is true i get that the data variable contains "true", but the check:
data == "true"
fails anyway. The code seem somehow to evaluate "true" == "true" to false.
success: function(data) {
if (data == "true") {
emailAlreadyIsUsed = true;
} else {
emailAlreadyIsUsed = false;
}
}
Does anybody know what this depend on?
I am new to develop in javascript, is there a good way to debug this? I have steped through this code in chrome checking the values of the variables.
It's because it's not a String, try data == true or if(data){... should work. How is it being set server side?
If you want to compare string with other string you can do this in following way:
success: function(data) {
emailAlreadyIsUsed = (data.indexOf('true') === 0);
}
But it is really strange that your code not working.
You can try strict comparison ===.
success: function(data) {
if (data === "true") {
emailAlreadyIsUsed = true;
} else {
emailAlreadyIsUsed = false;
}
}
Or try to compare two strings:
success: function(data) {
if (String(data) === "true") {
emailAlreadyIsUsed = true;
} else {
emailAlreadyIsUsed = false;
}
}
Thanks all for your answers.
What I in the end made to make this work is to change the dataType to "json". Then the variable data contains a boolean variable which I can normaly use.
How to get the return value true and false.
I always get undefined and after read some article i still didn't understand.
My reference:How to return value from an asynchronous callback function?
function validateScreenName(value){
var screenname = document.getElementById("screenname-box").value;
if((screenname.length < 3) || (screenname.length > 20)){
value(false); // if I change to return false still can't get the value. Always undefined.
}else{
if(screenname != "something"){
var http = new XMLHttpRequest();
var param = "screen_name="+screenname;
var url = "screen-name-validation.php";
http.open("POST",url,true);
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.onreadystatechange = function(){
//The response from server is either 1 or 0
if (http.readyState==4 && http.status==200){
if(http.responseText == 0){
value(true); // I can't get this value
}else{
value(false);
}
}
}
http.send(param);
}else{
value(false);
}
}
}
I need this value to this script
function disableSubmit(){
validateScreenName(function(val2){
var val1 = validateFullName();
var val3 = validateTargetName();
//I need val2 from validateScreenName
if(val1 && val2 && val3){
//if all true do something
}else{
//if one of the value false do somthing
}
});
}
Can someone explain how to get the value with only javascript without jquery? thanks.
Instead of returning typeof bool(false,true), use returning string..eg.,"true" or "false"
I have a issue in getting response in Kony application. this is the code
function getServerResponceOption(url){
alert(url);
var httpclient2 = new kony.net.HttpRequest();
var requestMethod = constants.HTTP_METHOD_GET;
var async = true;
httpclient2.open(requestMethod, url,async);
if(getAccessToken() != null && getAccessToken() != ""){
httpclient2.setRequestHeader("AuthToken",getAccessToken());
}
httpclient2.send();
httpclient2.onReadyStateChange = HandleResponce(httpclient2);
}
function HandleResponce(obj)
{
alert("Getting data "+obj.readyState+" Status "+obj.status+" Response "+obj.response );
if(obj.readyState == 4 )
{
if (obj.response != null && obj.response != "")
{
var jsonObj = obj.response;
handleResponseOption(0,jsonObj);
return;
}
else
{
}
}else{
var state = obj.status;
alert("Readystate "+obj.readyState+" Status = "+state);
}
if (obj.response != null && obj.response != "")
{
var jsonObj = obj.response;
handleResponseOption(1,jsonObj);
}
}
Here i got server response if i put the alert message in HandleResponce(obj) without the alert i didn't get any response. the ready state is 1 and status is 0. What is the problem occurred if i remove the alert message?
Note: URL and getAccessToken() is getting values.
You are calling function in line, When you use HandleResponce(httpclient2) function is immediately executed.
httpclient2.onReadyStateChange = HandleResponce(httpclient2);
Change your code as
httpclient2.onReadyStateChange = function(){ HandleResponce(httpclient2) };
I have a problem with this code:
setInterval(function() {
var timed = $("#chMsgCont").find(".chatMsg:last-child");
var timee = timed.attr("data");
$.ajax({
url: "./ajax/checknew.php",
data: {timestamp: timee},
success: function(data) {
$("#chMsgCont").append(data);
if(data != null) {
var div = $('#chMsgCont');
var o = div.offset().top;
div.scrollTop( o + 12302012 );
}
}
});
},1000);
Even if data is null, $("#chMsgCont) scrolls down, why?
Try it this way:
if(data)
Or this way:
if(data != null && data != "")
Basically, if(data) is considered as data is false in these cases:
data is "" or '' => empty string
data is false
data is null
data is undefined
data is just empty and not null (undefined) so it will pass, try returning something extra in your PHP file.
I prefer doing the following at PHP side:
/* suppose that i am evaluating an insert to my DB */
print(1); //if success
print(0); //if error
then at javascript side, do this:
if(data == 1) //means success
or just do the following if you are not evaluating something at PHP side:
if(data != null && data != '')