通过 AJAX 调用返回非字符串的 Web 方法 [ASP.net]

发布于 2025-01-07 16:57:57 字数 958 浏览 2 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

画▽骨i 2025-01-14 16:57:57

试试这个。

function Test() {

    $.ajax({
        type: "POST",
        url: "Default.aspx/test",
        data: "",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (msg) {
            msg = msg.hasOwnProperty("d") ? msg.d : msg;
            $.each(msg, function (index, value) {
                $(".resultbox").append("<p>" + value + "</p>");
            });
        }
    });

}

Try this.

function Test() {

    $.ajax({
        type: "POST",
        url: "Default.aspx/test",
        data: "",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (msg) {
            msg = msg.hasOwnProperty("d") ? msg.d : msg;
            $.each(msg, function (index, value) {
                $(".resultbox").append("<p>" + value + "</p>");
            });
        }
    });

}
枯寂 2025-01-14 16:57:57

我相信 DOM 异常与通过 jQuery 的 html() 方法将数组对象传递到 innerHTML() 相关。首先尝试将数组值格式化为字符串:

success: function (msg) {
  // Builds a string like '1s, 2s'
  var formattedMessage = msg.d.join(', ');

  $(".resultbox").html(formattedMessage);
}

当然,手动构建格式化字符串并不有趣。为了改善这种情况,请研究客户端模板解决方案,例如 jQuery Templates 及其后继者 JsRender。

I believe that DOM exception is related to passing an array object into innerHTML(), via jQuery's html() method. Try formatting your array values as a string first instead:

success: function (msg) {
  // Builds a string like '1s, 2s'
  var formattedMessage = msg.d.join(', ');

  $(".resultbox").html(formattedMessage);
}

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.

我的鱼塘能养鲲 2025-01-14 16:57:57

由于您要从 WebMethod 发送字符串列表,因此 msg 将是一个数组。在这种情况下,msg.d 将为您提供 undefined 结果。如果您想将它们显示为以逗号分隔的组合字符串,请尝试此操作。

ajax 本质上是异步的,默认情况下,您无法从 Test 方法返回 ajax 响应。如果您想返回它,请将async设置为false,它将等待响应到来,然后您可以返回它。

function Test() {
            var x = null;//Define variable x
            $.ajax({
                type: "POST",
                async: false,
                url: "webservice.asmx/test",//Web service url
                data: "",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (msg) {
                    x = msg;
                    $(".resultbox").html(msg.join(','));
                }
            });

            return x;
}

Since you are sending a list of string from the WebMethod so msg will be an array. In that case msg.d will give you undefined 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 from Test method. If you want to return it then set async to false which will wait until the response comes and then you can return it.

function Test() {
            var x = null;//Define variable x
            $.ajax({
                type: "POST",
                async: false,
                url: "webservice.asmx/test",//Web service url
                data: "",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (msg) {
                    x = msg;
                    $(".resultbox").html(msg.join(','));
                }
            });

            return x;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文