通过 AJAX 调用返回非字符串的 Web 方法 [ASP.net]
我在 C# 文件中创建了一个名为 test
的 Web 函数,它返回一个简单的列表进行测试
[WebMethod(EnableSession = false)]
public static List<string> test()
{
List<string> a = new List <string>() ;
a.Add("1s");
a.Add("2s");
return a;
}
,
function Test() {
$.ajax({
type: "POST",
url: "Default.aspx/test",
data: "",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
x = msg;
$(".resultbox").html(msg.d);
}
});
return x;
}
当我调用 test( );来自控制台的消息是:
Uncaught Error: NOT_FOUND_ERR: DOM Exception 8 jquery-1.6.1.min.js:18
在常见情况下,我将对象转换为 JSON 并返回它们,这通常可以工作,但我有兴趣知道对象如何从 WEBMETHOD 返回到前端,它们是否序列化为文本,为什么会出现此错误发生了。
i made a web function in the C# file called test
it returns a simple List for testing
[WebMethod(EnableSession = false)]
public static List<string> test()
{
List<string> a = new List <string>() ;
a.Add("1s");
a.Add("2s");
return a;
}
i tried to call this WEBMETHOD
from the front end using JQUERY AJAX
function Test() {
$.ajax({
type: "POST",
url: "Default.aspx/test",
data: "",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
x = msg;
$(".resultbox").html(msg.d);
}
});
return x;
}
when i call test() ; from the console the message was :
Uncaught Error: NOT_FOUND_ERR: DOM Exception 8 jquery-1.6.1.min.js:18
in common cases i convert objects into JSON and return them and this usually work but i'm interested to know how objects are returned from the WEBMETHOD to the front end are they serialized into text , why did this error happened .
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
试试这个。
Try this.
我相信 DOM 异常与通过 jQuery 的
html()
方法将数组对象传递到innerHTML()
相关。首先尝试将数组值格式化为字符串:当然,手动构建格式化字符串并不有趣。为了改善这种情况,请研究客户端模板解决方案,例如 jQuery Templates 及其后继者 JsRender。
I believe that DOM exception is related to passing an array object into
innerHTML()
, via jQuery'shtml()
method. Try formatting your array values as a string first instead:Manually building the formatted string is no fun, of course. To improve that situation, look into client-side templating solutions like jQuery Templates and its successor JsRender.
由于您要从
WebMethod
发送字符串列表,因此msg
将是一个数组。在这种情况下,msg.d
将为您提供undefined
结果。如果您想将它们显示为以逗号分隔的组合字符串,请尝试此操作。ajax
本质上是异步的,默认情况下,您无法从Test
方法返回 ajax 响应。如果您想返回它,请将async
设置为false
,它将等待响应到来,然后您可以返回它。Since you are sending a list of string from the
WebMethod
somsg
will be an array. In that casemsg.d
will give youundefined
result. If you want to show them as a combined string seperated by comma then try this.ajax
is async in nature by default you cannot return the ajax response fromTest
method. If you want to return it then setasync
tofalse
which will wait until the response comes and then you can return it.