[英]How to change client side javascript 's variable from server side?
I took inspiration from a code of a login page from a certain website. 我从某个网站的登录页面代码中汲取了灵感。 I find a function which I do not know how to implement.
我找到了一个我不知道如何实现的功能。
That login page uses two input controls in a form to gather username and password: 该登录页面使用两种输入控件的形式收集用户名和密码:
<form method="post" runat="server" action="myLogin.aspx">
<input type="text" class="us_name" name="txtUserName" id="txtUserName" value="" />
<input type="password" class="us_pwd" name="txtPassword" id="txtPassword" value="" />
</form>
and in that page, I also find some JQuery code like this: 在该页面中,我还找到了一些类似以下的JQuery代码:
<script language="javascript" type="text/javascript">
$(document).ready(function () {
var error = 0;
if (error > 0) {
switch (error) {
case 1:
$("#ListMsg").html("username and pwd can not be empty");
$("#txtUserName").focus();
break;
case 2:
$("#ListMsg").html("Pic code can not be empty");
$("#txtCheckCode").focus();
break;
case 3:
$("#ListMsg").html("Pic code err");
$("#txtCheckCode").focus();
break;
case 36006:
$("#ListMsg").html("username err");
$("#txtCheckCode").focus();
break;
case 36007:
$("#ListMsg").html("password err");
$("#txtCheckCode").focus();
break;
case 20012:
$("#ListMsg").html("account is lock");
$("#txtCheckCode").focus();
break;
case 10007:
$("#ListMsg").html("can not access");
$("#txtCheckCode").focus();
break;
default:
$("#ListMsg").html("username or password err");
$("#txtPassword").focus();
break;
}
}
});
It seems that above code is to display error message when login fails, but please notice this statement: 上面的代码似乎是在登录失败时显示错误消息,但是请注意以下语句:
var error = 0;
When I type a wrong password and view page source, I find it has automatically changed to : 当我输入错误的密码并查看页面源代码时,我发现它已自动更改为:
var error = 36007;
I guess this variable must be changed by server side code when login fails and use it to indicate fail reason. 我猜想登录失败时必须由服务器端代码更改此变量,并使用它来指示失败原因。 But I do not know how server can set client side JQuery variable's value, can anybody give me an example?
但是我不知道服务器如何设置客户端JQuery变量的值,有人可以举一个例子吗?
you can use ajax to facilitate communication between your script and php server without changing the page. 您可以使用ajax来促进脚本和php服务器之间的通信,而无需更改页面。 when user clicks login button, you can send his/her input values as an ajax post.
当用户单击登录按钮时,您可以将其输入值作为ajax帖子发送。 consider the following example:
考虑以下示例:
$( '#btn-login' ).click( function () {
$.ajax({
url: "your.server.address/function_that_sends_those_codes",
data: {
userID: $( '#txtUserName' ).val(),
pwd: $( '#txtPassword' ).val()
},
type: 'POST',
dataType: 'JSON',
success: function ( response ) {
error = response.errCode; //or whatever your server
//sends
},
error: function ( errorMsg ) {
console.log( errorMsg );
}
});
});
in your php code, you retrieve data sent using $_POST[ 'userID' ]
and $_POST[ 'pwd' ]
. 在您的php代码中,您检索使用
$_POST[ 'userID' ]
和$_POST[ 'pwd' ]
。 you calculate your code by interacting with database, and you do this: 您可以通过与数据库进行交互来计算代码,然后执行以下操作:
echo json_encode( array( yourErrorCode ) );
'yourErrorCode' is sent as 'response' to your 'success' callback function. “ yourErrorCode”作为“响应”发送到您的“成功”回调函数。 you can view the response received, or any error thrown, in the console of your browser.
您可以在浏览器的控制台中查看收到的响应或引发的任何错误。
happy coding :) 快乐的编码:)
To set javascript variable from code behind you can register a client script . 要从后面的代码中设置javascript变量,您可以注册客户端脚本 。 like this:
像这样:
ScriptManager.RegisterStartupScript(this, this.GetType(), "", "error = 36007;", true);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.