0
votes

I want to set one value in server side session in client side and need to access that session in web service, so i tried below

In client side :

//Set the server side session like below  
 var vr_="demo.png";     
'<%Session["path"] = "' + vr_ + '"; %>';

//In alert,checked the server side session value like below
 alert('<%=Session["path"] %>');

the value getting in alert is fine and i called the web service but when try to access the session in web service i am getting the following as session value

' + vr_ + ' not the original value "demo.png"

Please find the source code below,

In Javascript :

  var vr_="G:\\13-06-15-demo\\app1\\uploads\\demo.png"

In Jquery ajax request, i called the webservice like below

 $.ajax({
        url: "../webservice/email.asmx/senmail",
        data: "{'tomail': '" + tomail + "','subject': '" + subject + "','message': '" + message + "','attachment': '" + vr_+ "'}",           
        dataType: "json",
        type: "POST",
        contentType: "application/json; charset=utf-8",

In web service, the method is like below

 public bool senmail(string toMailId, string subject, string message,string attachment)
    {
1
You cannot set session from client side, at all. - John Saunders
in that case,i need to send one image path to web service, so even tried to passed in parameters like these "G:\\13-06-15-demo\\app1\\uploads\\demo.png" but am getting unrecognised escape sewuence error - Nandha
@NandhaKumar can you post he code you are using? - fsacer
Try not doubling up the `\`. - John Saunders
also try again saving data as array and then generate JSON string using JSON.stringify like in this example stackoverflow.com/questions/6323338/… . I also think it makes code a whole lot more readable. - fsacer

1 Answers

0
votes

You've got a quotes problem, fix it like this:

<% Session["path"] = "'" + vr_ + "'"; %>

EDIT 1:
Javascript and ASP.NET are not the same, so you cannot access the variables, so you can't do it on the client side. You must send something to the server like a POST request using Javascript or easier using jQuery.

Here are some resources:

Using AJAX and Javascript:
http://www.w3schools.com/php/php_forms.asp http://www.w3schools.com/ajax/ajax_xmlhttprequest_send.asp

jQuery AJAX: http://api.jquery.com/jQuery.ajax/
http://api.jquery.com/jQuery.post/

EDIT 2:
The answer was to create a normal javascript object/array and then convert it to JSON using JSON.stringify(), like seen in this example: Jquery Ajax Posting json to webservice.

JSON.stringify(): https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify