I’ve got a webmethod in .NET that returns a List of objects.
[System.Web.Services.WebMethod(enableSession: true)]
public static List<CourseItem> GetItems() {
return (new MTE()).GetItems();
}
And the CourseItem object as follows:
public static CourseItem {
public string CourseNum { get; set; }
public string Desc { get; set; }
}
I consume the webmethod from javascript like thus:
function GetCourses_Success(result) {
// Problem starts here
if (result.d != null) {
for (i = 0; i < result.d.length; i++) {
alert(result.d[i]["CourseNum"]);
}
PROBLEM:
The Alert sometimes says ‘NaN’ instead of the actual course number. The problem has to do with the fact that most of the course numbers are just numbers, but there is the occasional course number that is a number with an asterisk at the end of it (eg – 111234* or 999234*). So somewhere along the way between the webmethod and the alert something is trying to be too smart for its own good and thinking that CourseNum is a number field (integer perhaps) when in reality its just a string.
I also tried
result.d[i]["CourseNum"].toString()
but the alert still says ‘NaN’
Thanks in advance for the help!!
Try escaping the asterisk. It’s quite possible that Javascript believes you are multiplying. As you aptly put it, something (Javascript) is too smart for its own good.