使用 jquery ajax asp.net 4.0 调用 asmx 服务

发布于 2024-10-14 13:54:54 字数 805 浏览 3 评论 0原文

我正在尝试使用 jquery 调用示例 asmx 服务,这里是 jquery 代码

$.ajax({
            type: "POST",
            url: "/Services/Tasks.asmx/HelloWorld",
            data: "{}",
            dataType: "json",
            contentType: "application/xml; charset=utf-8",
            success: function (data) {                   
                alert(data);                    
            }
        });

这没有显示任何消息,代码位于 asp.net 4.0 中, 我错过了什么吗?

编辑 - 我将 dataType 更改为 xml,现在成功函数正在工作,它返回以下 xml

<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://tempuri.org/">Hello World</string>

我正在使用以下代码来解析 xml 数据,它在警报中显示 null

success: function (data) {
    edata = $(data).find("string").html();
    alert(data);
}

I'm trying to call a sample asmx service using jquery, here is the jquery code

$.ajax({
            type: "POST",
            url: "/Services/Tasks.asmx/HelloWorld",
            data: "{}",
            dataType: "json",
            contentType: "application/xml; charset=utf-8",
            success: function (data) {                   
                alert(data);                    
            }
        });

This is not showing any message,code is in asp.net 4.0,
Am I missing any thing?

Edit - I changed the dataType to xml, now success function is working it return following xml

<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://tempuri.org/">Hello World</string>

I'm using following code to parse xml data and it is showing null in alert

success: function (data) {
    edata = $(data).find("string").html();
    alert(data);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

孤者何惧 2024-10-21 13:54:54

我相信这是因为您有 dataType: "json" 并且它期望响应内容类型相同,但正在返回 XML。我敢打赌整个事件正在发起,但没有成功。

尝试

$.ajax({
            type: "POST",
            url: "/Services/Tasks.asmx/HelloWorld",
            data: "{}",
            dataType: "json",
            contentType: "application/xml; charset=utf-8",
            success: function (data) {                   
                alert(data);                    
            },
            complete: function (data) {                   
                alert(data);                    
            }
        });

更新

我认为这是因为你正在使用 .html(),你需要使用 text()。另外,我不知道您是否打算这样做,但您的警报中有data,我假设您打算使用edata。以下对我有用:

jQuery.ajax({
    type: "POST",
    url: "/yourURL",
    dataType: "xml",
    data: "{}",
    contentType: "application/xml; charset=utf-8",
    success: function(data) {
        edata = $(data).find("string").text();
        alert(edata);
    }
})

I believe it's because you have the dataType: "json" and it's expecting the response content-type to be the same but XML is being returned. I bet the complete event is being raised but not success.

try

$.ajax({
            type: "POST",
            url: "/Services/Tasks.asmx/HelloWorld",
            data: "{}",
            dataType: "json",
            contentType: "application/xml; charset=utf-8",
            success: function (data) {                   
                alert(data);                    
            },
            complete: function (data) {                   
                alert(data);                    
            }
        });

UPDATE

I think it's because you're using .html(), you need to use text(). Also i don't know if you meant to do it or not but you have data in your alert, i'm assuming you meant to use edata. The following worked for me:

jQuery.ajax({
    type: "POST",
    url: "/yourURL",
    dataType: "xml",
    data: "{}",
    contentType: "application/xml; charset=utf-8",
    success: function(data) {
        edata = $(data).find("string").text();
        alert(edata);
    }
})
月棠 2024-10-21 13:54:54

我建议将 [ScriptService] 属性添加到您的 Tasks.asmx 类中,以便它将以 JSON 而不是 XML 的形式接受和响应。您的客户端代码看起来不错,但您需要查看成功处理程序中的“data.d”而不是“data”。

I'd recommend adding the [ScriptService] attribute to your Tasks.asmx class so it will accept and respond in JSON instead of XML. Your client code looks good, but you'll want to take a look at "data.d" instead of "data" in your success handler.

酷到爆炸 2024-10-21 13:54:54
  use it.

   <script>
        alert("aaa");
    $.ajax({
        type: "POST",
        url: "MyService.asmx/HelloWorld",
        data: "{}",
        dataType: "xml",
        contentType: "application/xml; charset=utf-8",
        success: function (data) {
        alert(data);//data-object xmldocument
        edata = $(data).children("string").text();
        alert(edata);

        }
    });
    alert("bbb");
    </script>
  use it.

   <script>
        alert("aaa");
    $.ajax({
        type: "POST",
        url: "MyService.asmx/HelloWorld",
        data: "{}",
        dataType: "xml",
        contentType: "application/xml; charset=utf-8",
        success: function (data) {
        alert(data);//data-object xmldocument
        edata = $(data).children("string").text();
        alert(edata);

        }
    });
    alert("bbb");
    </script>
绝不服输 2024-10-21 13:54:54

好吧,您声明 dataType 是 JSON,但 contentType 是 XML。尝试

contentType: "application/json; charset=utf-8",

如果没有,那么我们就必须查看 asmx 代码。

Well, you're stating that the dataType is JSON, but the contentType is XML. Try

contentType: "application/json; charset=utf-8",

If not, then we'd have to see the asmx code.

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