如何在javascript中创建会话并在controller mvc中访问它
我有一个mvc项目。我想用javascript创建一个会话,并能够在页面的控制器中访问它。 我尝试过不同的选择,但都不适合我。。。 我试过的一件事是: 视图: 有什么想法吗如何在javascript中创建会话并在controller mvc中访问它,javascript,asp.net-mvc,session,controller,Javascript,Asp.net Mvc,Session,Controller,我有一个mvc项目。我想用javascript创建一个会话,并能够在页面的控制器中访问它。 我尝试过不同的选择,但都不适合我。。。 我试过的一件事是: 视图: 有什么想法吗 $(function(){ $.ajax({ url :'ajax page url', method :'get/post', data : variable containing data, success:function(html) {
$(function(){
$.ajax({
url :'ajax page url',
method :'get/post',
data : variable containing data,
success:function(html)
{
alert('success');
}
})
});
除了在ajax页面的会话中添加值之外,Javascript是服务器端,您还必须通过ajax将变量传递给服务器 执行ajax请求的最简单方法是使用jQuery库 以下是如何在jQuery中执行ajax请求:
$(function () {
$.ajax({
method: "POST"
,data: {
sessionVariable: variable
}
,url: //enter the url of your controller/action which the session variable is being sent to, and handled by the server.
,success: function(returnedData){
alert("Session successfully sent to the server");
// you could return something from the controller with more info and display this using the returnedData object.
}
,error: function(){
alert("something went wrong");
}
});
})
有关jQuery ajax的更多信息,请点击此链接>会话是服务器端的。Javascript是客户端。您需要将该值传递给服务器,例如使用ajax并在controllerok中进行设置。我真的不知道如何使用ajax。你能给我更具体的指示吗?感谢您有时间做一些研究:
$(function(){
$.ajax({
url :'ajax page url',
method :'get/post',
data : variable containing data,
success:function(html)
{
alert('success');
}
})
});
$(function () {
$.ajax({
method: "POST"
,data: {
sessionVariable: variable
}
,url: //enter the url of your controller/action which the session variable is being sent to, and handled by the server.
,success: function(returnedData){
alert("Session successfully sent to the server");
// you could return something from the controller with more info and display this using the returnedData object.
}
,error: function(){
alert("something went wrong");
}
});
})