Search code examples
jqueryasp.netajaxserver-variables

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.


Solution

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

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