1

今天刚开始学习jQuery ajax,按照教程说的但是没有用。

HelloWorld是方法名,但是根据报错信息,好像不是方法名而是页面名。

jQuery

$(document).ready(function () {
    //alert("hello world");
    $('.ordernumber').on('click', function () {
        var orderNum = $(this).text();
        $.ajax({
            type: "POST",
            url: "./OrderDetail.asmx/HelloWorld",
            data: "{}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (msg) {
                alert(msg);
                // Do interesting things here.
            }
        });
        //alert($(this).text());

    });
});

OrderDetail.asmx.vb

Imports System
Imports System.Web.Services

Public Class OrderDetail
Inherits System.Web.Services.WebService

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

End Class

错误信息:

POST http://localhost:64616/OrderDetail.asmx/HelloWorld 500 (Internal Server Error)
4

2 回答 2

3

我认为您需要添加<System.Web.Script.Services.ScriptService()>到您的课程中;

<System.Web.Script.Services.ScriptService()> _
Public Class OrderDetails
    Inherits System.Web.Services.WebService

    '' rest of your code

End Class

同样要返回 Json,您需要使用以下方法装饰您的方法;

<ScriptMethod(ResponseFormat:=ResponseFormat.Json)> _

更新

创建新的 ASMX Web 服务时,默认代码状态;

' To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
' <System.Web.Script.Services.ScriptService()> _
于 2013-08-21T21:19:59.367 回答
1

您期望返回 JSON,但 asmx Web 服务返回 XML,而不是您需要添加

<WebMethod()> _
<ScriptMethod(ResponseFormat:=ResponseFormat.Json)>_
Public Function HelloWorld() As String
 Return "Hello World"
End Function

更多解释的链接

于 2013-08-21T21:22:48.000 回答