简体   繁体   English

如何在JS / JQuery后面的C#代码中定义未定义的返回值

[英]How to define an undefined return value from C# code behind, JS / JQuery

This is a follow up using the code from my previous question: 这是使用上一个问题中的代码的后续操作:

Why wont js read text box values 为什么JS不会读取文本框值

My code behind methods are already displayed in that post. 我在方法后面的代码已经显示在该帖子中。

My script works fine with the code behind but when I am trying to retrieve the return value I am getting a return value of "undefined". 我的脚本与后面的代码配合良好,但是当我尝试检索返回值时,得到的返回值为“ undefined”。

The JS code is simple: JS代码很简单:

var answer  = PageMethods.saveCat(CategoryName, ParentID, CategoryID, CatChk);
window.alert(answer);

I have also tried: 我也尝试过:

windows.alert(String(answer));

but I keep getting an undefined value. 但我一直得到一个未定义的值。 I have searched around but have only been able to find solutions using a drawn out $.ajax syntax and was wondering is there a simple way to grab the string value returned by my web method similar to the code I am trying to get to work instead of writing 10 - 15 lines of code? 我四处搜索,但只能使用绘制的$ .ajax语法找到解决方案,并且想知道是否有一种简单的方法来获取我的Web方法返回的字符串值,类似于我尝试使用的代码编写10到15行代码?

Thanks for the help. 谢谢您的帮助。

Thanks to Karthik Ganesan's comment I was able to get on the right track using this link: mindfiresolutions.com/Using-PageMethods-in-ASPNET-2467.php 感谢Karthik Ganesan的评论,我可以使用此链接进入正确的轨道:mindfiresolutions.com/Using-PageMethods-in-ASPNET-2467.php

My resulting code was: 我得到的代码是:

PageMethods.saveCat(CategoryName, ParentID, CategoryID, CatChk, onSuccess, onFailure);
               function onSuccess(result)
               {
                   alert(result);
               }
               function onFailure(result)
               {
                   alert("There was an error saving the category.");
               }

I am posting this hoping it helps out other newbies to JS. 我发布这个希望是希望对其他新手有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM