User970008
User970008

Reputation: 1155

Jquery / .Net Passing a Server Variable in the Data of Ajax Call

I want to take a .Net server variable and user that in the data of an Ajax call.

$.ajax({
  url: "get_user_info.aspx",
  data: "user_id=<%=UserID%>" 
});

However, I get an Object Expected error with the above.

The UserID is an int in the C# codebehind.

I have tried casting it to string like this:

var useridstring = <%=UserID%>;
var mynewstring = useridstring.toString();

 $.ajax({
      url: "get_user_info.aspx",
      data: {user_id:mynewstring} 
    });

It is not working though. I've read posts here about using a hidden input with a variable, but I am hoping to avoid that method if something like the above can work.

Upvotes: 1

Views: 1556

Answers (2)

enricoariel
enricoariel

Reputation: 483

I would place the variable inside a hidden div and get from jquery the inner text of this div.

var useridstring = $get('myDiv');

<div id='myDiv' style="display:none;><%:UserID%></div>

Upvotes: 0

Diodeus - James MacFarlane
Diodeus - James MacFarlane

Reputation: 114367

Your field name (user_id) needs to be a string:

var useridstring = '<%=UserID%>';


 $.ajax({
      url: "get_user_info.aspx",
      data: {'user_id':useridstring} 
    });

Upvotes: 3

Related Questions