Reputation: 28336
I have a session variable containing some string data. How do I access the session variable from the markup (not the code behind) and pass its value to the following code? I need to replace the ????? with the actual value of the session variable. Thanks!
function btnAddTask_onclick() {
var inputData = { task: $(".taskInput").val(), sessionVar: ????? };
$.ajax({
"url": "AddTask.aspx",
"type": "get",
"data": inputData,
"success": function (response) {
alert(response);
$("body").append(response);
},
"error": function (response) {
alert("Error: " + response);
}
})
}
Upvotes: 0
Views: 3634
Reputation: 24344
@Carlos M's answer is good, assuming that the data you need from session is up to date at the time the button is clicked. But since you're using Ajax, I'm going to assume that is not necessarily true, e.g. some other previous ajaxified action on the client could have caused Session
to change.
What I can't help wondering, though, is what purpose does passing session data directly to an ajax request serve? Why don't you just access Session
directly from your code in AddTask.aspx
instead having the server render something on the client, only to pass it right back to the server? It doesn't seem as if there's any purpose in accessing Session
on the client, since all you do is pass it to the server again. It's not actually used at the client.
Upvotes: 2
Reputation: 81
var inputData = { task: $(".taskInput").val(), sessionVar: '<%= Session["session_var"] %>' }
Upvotes: 3