如何拆分字符串并将其存储在javascript中的cookie或localstorage中 [英] How to split string and store it in cookies or localstorage in javascript

查看:78
本文介绍了如何拆分字符串并将其存储在javascript中的cookie或localstorage中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

< pre>< / pre> 我是PhoneGap的新手,在开发之前做了一些研究。我已通过网站注册测试应用程序,因此我需要使用我的凭据进行测试,看看是否可以登录。我从服务器获取包含sessionid和登录人名的字符串。所以我需要将其保存在cookie中,其中

<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不支持保存cookie中的数据,因此如果用户登录或注销,需要将其存储在localStorage中。



我正在使用html5,CSS和javascript。



我的HTML代码

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>







我得到的字符串格式为 43330KDDD |姓名|

所以我需要将其存储在本地存储中

作为会话,其

名称




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>






帮助将不胜感激。



我尝试了什么:



当我尝试使用ajax时它给了我错误提醒



函数Goin(){



var form = $(# goin);



var url ='https://www.test.login';

var email = $(#电子邮件)。val();

var password = $(#password)。val();

var loginPath = url +'testlogin?'+ email +'|'+密码+'|';

if(email!=&& password!=){

$ .ajax({

类型:'POST',

url:loginPat h,

crossDomain:true,

data:{email:email,password:password},

dataType:'json',

async:false,

//如果在cookie中登录成功存储会话ID

成功:函数(响应){

console.log('response');

alert(response);

if(response.success){

alert( 你已登录);

console.log(你已登录);

window.localStorage.setItem(sessionId,value );

var sessionId = window.localStorage.getItem(sessionId);

window.location.replace(index.html);

}

else {

// alert(你的登录失败);

console.log('你的登录失败' ');

window.location(signin.html);

}

},

错误:function(error){

//alert(response.success);

console.log('无法连接到服务器');

alert('无法连接到服务器'+错误);

window.location =signin.html;

}

});

}

else {

//如果电子邮件和密码是空的

console.log('你必须输入电子邮件和密码')

alert(你必须输入电子邮件和密码);

}

返回false;

}



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;
}

推荐答案

(#goin);



var url ='https://www.test.login';

var email =
("#goin");

var url = 'https://www.test.login';
var email =


(#email)。val();

var password =
("#email").val();
var password =


(#密码)。val();

var loginPath = url +'testlogin?'+ email +'|'+ password +'|';

if(email! =&&密码!=){
("#password").val();
var loginPath = url + 'testlogin?' + email + '|' + password + '|';
if(email != "" && password != "") {


这篇关于如何拆分字符串并将其存储在javascript中的cookie或localstorage中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆