代码之家  ›  专栏  ›  技术社区  ›  clockwiseq

在母版页上调用jqueryajax?

  •  3
  • clockwiseq  · 技术社区  · 14 年前

    我错过什么了吗?我正在尝试使用jquery对我的站点上的web服务进行一个简单的ajax调用,每次从母版页进行调用时都会出现500个错误。难道从来没有人从母版页上调用过ajax吗?或者我只是疯了,睡不着觉?

    例子:

    母版页.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>
    

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

    有什么不对劲吗?我对母版页有误解吗?请帮帮我!

    1 回复  |  直到 14 年前
        1
  •  4
  •   clockwiseq    14 年前

    好的,我想我已经解决了问题。现在我开始觉得我只是困了。我有几个问题,我会列出给其他人,以确保他们不会在未来这样做:

    $.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) 一旦我解决了jqueryajax问题,就会收到来自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
    

    这解决了它,现在我可以把它叫到任何地方。谢谢你的帖子,但看起来我只是像往常一样忽略了事情。。。