Вопрос

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.

Это было полезно?

Решение

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

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


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

Другие советы

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>
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top