在母版页上进行 jQuery Ajax 调用?

发布于 2024-09-18 12:05:29 字数 950 浏览 4 评论 0原文

我错过了什么吗?我正在尝试使用 jquery 对我网站上的 Web 服务进行简单的 ajax 调用,每次从母版页进行调用时都会收到 500 错误。没有人从母版页进行过 ajax 调用,还是我只是疯了并且极度缺乏睡眠?

示例:

MasterPage.master

<script type="text/javascript">
    $(document).ready(function () {
        $.ajaxSetup({ dataType: "json", contentType: "application/json; charset=utf-8" });

        $.ajax({
            url: '<%= ResolveUrl("~/Services/Test.asmx/HelloWorld") %>',
            success: function (data) {
                alert(data);
            },
            error: function (xhr, err) {
                alert("readyState: " + xhr.readyState + "\nstatus: " + xhr.status);
                alert("responseText: " + xhr.responseText);
            }
        });

    });
</script>

/Services/Test.asmx

<WebMethod()> _
Public Function HelloWorld() As String
   Return "Hello World"
End Function

看到什么问题了吗?我对母版页有误解吗?请帮忙!

Am I missing something? I'm trying to do a simple ajax call using jquery to a web service on my site and I get a 500 error every time when I make the call from the master page. Has no one ever made an ajax call from a master page or am I just crazy and extremely deprived of sleep?

Example:

MasterPage.master

<script type="text/javascript">
    $(document).ready(function () {
        $.ajaxSetup({ dataType: "json", contentType: "application/json; charset=utf-8" });

        $.ajax({
            url: '<%= ResolveUrl("~/Services/Test.asmx/HelloWorld") %>',
            success: function (data) {
                alert(data);
            },
            error: function (xhr, err) {
                alert("readyState: " + xhr.readyState + "\nstatus: " + xhr.status);
                alert("responseText: " + xhr.responseText);
            }
        });

    });
</script>

/Services/Test.asmx

<WebMethod()> _
Public Function HelloWorld() As String
   Return "Hello World"
End Function

See anything wrong? Do I have a misunderstanding of the Master Page? Please help!

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

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

发布评论

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

评论(1

厌味 2024-09-25 12:05:29

好吧,我相信我已经解决了这个问题。现在我开始认为我只是困了。我遇到的几个问题,我将向其他人列出它们,以确保它们将来不会这样做:

1) 我记得之前读过另一篇文章,其中解释了通过 jQuery 库进行 ajax 调用确实如此不像数据的空对象,所以即使它是一个空数组也必须列出一些内容。所以,这正是我添加的内容:

$.ajax({
            dataType: "json",
            contentType: "application/json; charset=utf-8",
            type: 'POST',
            data: [],
            url: '<%= ResolveUrl("~/Test.asmx/HelloWorld") %>',
            success: function (data) {
                alert(data);
            },
            error: function (xhr, err) {
                //alert("readyState: " + xhr.readyState + "\nstatus: " + xhr.status);
                //alert("responseText: " + xhr.responseText);
                $('#ajaxResponse').html(xhr.responseText);
            }
        });

2) 一旦解决了 jQuery ajax 问题,我就会收到来自 Web 服务本身的错误消息。警告我,为了能够从脚本调用 Web 服务,我必须添加以下行:

<System.Web.Script.Services.ScriptService()> _
<System.Web.Services.WebService(Namespace:="http://tempuri.org/")> _
<System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<ToolboxItem(False)> _
Public Class Test
    Inherits System.Web.Services.WebService

    <WebMethod()> _
    Public Function HelloWorld() As String
        Return "[{""Hello World""}]"
    End Function

End Class

这解决了这个问题,现在我可以在它所在的任何地方调用它。感谢您的帖子,但看起来我只是像往常一样忽略了事情......

Ok, I believe I have figured out the problem. Now I'm beginning to think I am just sleepy. A couple of issues that I had and I'll list them for everyone else to make sure they DON'T do in the future:

1) I remember reading another post previously that explained that an ajax call via the jQuery library does not like a null object for data so something has to be listed even if it's an empty array. So, that's exactly what I added:

$.ajax({
            dataType: "json",
            contentType: "application/json; charset=utf-8",
            type: 'POST',
            data: [],
            url: '<%= ResolveUrl("~/Test.asmx/HelloWorld") %>',
            success: function (data) {
                alert(data);
            },
            error: function (xhr, err) {
                //alert("readyState: " + xhr.readyState + "\nstatus: " + xhr.status);
                //alert("responseText: " + xhr.responseText);
                $('#ajaxResponse').html(xhr.responseText);
            }
        });

2) Once I got past the jQuery ajax problem, I was then presented an error message from the web service itself. Warned me that to be able to call the web service from a script, I have to add the following line:

<System.Web.Script.Services.ScriptService()> _
<System.Web.Services.WebService(Namespace:="http://tempuri.org/")> _
<System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<ToolboxItem(False)> _
Public Class Test
    Inherits System.Web.Services.WebService

    <WebMethod()> _
    Public Function HelloWorld() As String
        Return "[{""Hello World""}]"
    End Function

End Class

This solved it and now I can call it wherever it resides. Thanks for the posts but looks like I was just overlooking things as usual...

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