Click here to Skip to main content
15,881,139 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
<pre></pre>I'm newbie to PhoneGap, have done some research before starting on development. I have registered through the website site to test app, so I need to test with my credentials and see if can will login. I do get the string from server which contains sessionid and name of person login in. So I need to save that in cookies, of which

phonegap do not support the saving of data in cookies so need to store it in localStorage to now if user is login or logout.

I'm using html5, CSS and javascript.

My html code
<pre><form id="goin" name="goin" class="goin" onclick="Goin()" >
     <h3>Sign In</h3>
     <input type="email" name="email" id="email" required="required" class="email" value="" Placeholder="Email Address" >
     <input type="password" name="password" id="password" class="password" required="required" value="" placeholder="Password" >
     <input type="submit" id="login" name="login" class="login" value="Login"><br/>
    <div class="noaccount">
           <b>No account?</b><a href="register.html">Sign Up</a>
   </div>
</form>



String that I get is in this format 43330KDDD|Name|
So I need to store that in local storage
As Session with its value
Name with the value
<pre><pre><pre>



Help will be appreciated.

What I have tried:

When I tried to use ajax it gives me the error alert

function Goin() {

var form = $("#goin");

var url = 'https://www.test.login';
var email = $("#email").val();
var password = $("#password").val();
var loginPath = url + 'testlogin?' + email + '|' + password + '|';
if(email != "" && password != "") {
$.ajax({
type: 'POST',
url: loginPath,
crossDomain: true,
data: {email: email, password :password},
dataType: 'json',
async: false,
//if login success store session id in cookie
success: function (response){
console.log('response');
alert ("response");
if (response.success) {
alert("You're logged in");
console.log("You're logged in");
window.localStorage.setItem("sessionId", "value");
var sessionId = window.localStorage.getItem("sessionId");
window.location.replace("index.html");
}
else {
//alert("Your login failed");
console.log('Your login failed');
window.location("signin.html");
}
},
error: function(error){
//alert(response.success);
console.log('Could not connect to the server');
alert('Could not connect to the server' + error);
window.location = "signin.html";
}
});
}
else {
//if the email and password is empty
console.log('You must enter email and password')
alert("You must enter email and password");
}
return false;
}
Posted
Updated 21-Sep-16 2:23am
v3
Comments
Karthik_Mahalingam 15-Sep-16 11:03am    
remove the quotes in url : 'postDataUrl'
iDoKin 15-Sep-16 11:42am    
Okay I will try that.
Karthik_Mahalingam 15-Sep-16 11:44am    
does this works? never seen this kind of code til now in ajax call
var postData = $(this).serialize();
iDoKin 15-Sep-16 13:01pm    
XMLHttpRequest cannot load https://www.example.com/remotelogin?test@example.co.za|pass|. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://127.0.0.1:51234' is therefore not allowed access.

Yes its now working after I remove the quotes in the url

But I have this error now about header is present on the requested resource. Origin 'http://127.0.0.1:51234' is therefore not allowed access.
Karthik_Mahalingam 15-Sep-16 13:50pm    
check this
http://stackoverflow.com/a/20035319

1 solution

use window.sessionStorage.setItem(key,value) function of jquery to store your strings throughout your application.
 
Share this answer
 
Comments
iDoKin 20-Sep-16 4:36am    
And my login javascript the updated code.

`function Goin() {

var form = $("#goin");

var url = 'https://www.test.login';
var email = $("#email").val();
var password = $("#password").val();
var loginPath = url + 'testlogin?' + email + '|' + password + '|';
if(email != "" && password != "") {
$.ajax({
type: 'POST',
url: loginPath,
crossDomain: true,
data: {email: email, password :password},
dataType: 'json',
async: false,
//if login success store session id in cookie
success: function (response){
console.log('response');
alert ("response");
if (response.success) {
alert("You're logged in");
console.log("You're logged in");
window.localStorage.setItem("sessionId", "value");
var sessionId = window.localStorage.getItem("sessionId");
window.location.replace("/index.html");
}
else {
alert("Your login failed");
console.log('Your login failed');
window.location("signin.html");
}
},
error: function(error){
//alert(response.success);
console.log('Could not connect to the server');
alert('Could not connect to the server' + error);
window.location = "signin.html";
}
});
}
else {
//if the email and password is empty
console.log('You must enter email and password')
alert("You must enter email and password");
}
return false;
}`
iDoKin 20-Sep-16 4:40am    
Update HTML page code.


<form id="goin" name="goin" class="goin" onclick="Goin()" >     <h3>Sign In</h3>     <input type="email" name="email" id="email" required="required" class="email" value="" Placeholder="Email Address" >     <input type="password" name="password" id="password" class="password" required="required" value="" placeholder="Password" >     <input type="submit" id="login" name="login" class="login" value="Login"><br/>    <div class="noaccount">           <b>No account?</b><a href="register.html">Sign Up>   </div></form>



I get the long string from server, but the string is displayed in a iframe page, so challenge is getting hold of that string so in my javascript where can I put the code to get that string split it and save it in localSession? M a newbie to phonegap still learning. Help will be appreciated.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900