0

我已经在一个应用程序中创建了一个 Web 服务。现在我需要在我的另一个网站中使用它。我知道如果 Web 服务在同一个应用程序中,例如使用 jquery 提供像“webservicename.asmx/getInventoryForWs”这样的 url,它可以如何完成。我的 jquery 调用代码是

     $.ajax({
            type: "POST",
            url: "http://localhost/DashboardUI/Reports/Marketing/AUSUWs.asmx/getInventoryForWs",
            contentType: "application/json; charset="utf-8",
            dataType: "jsonp",
            success: function (ret) {
                alert('success');
            },
            error: function (httpRequest, textStatus, errorThrown) {
                alert("status=" + textStatus + ",error=" + errorThrown);
            }
        });    

这是我的 vb.net 网络服务代码

Imports System.Web
Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.Web.Script.Services

' To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the       
following line.
<System.Web.Script.Services.ScriptService()> _
<WebService(Namespace:="http://microsoft.com/webservices/")> _
<WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Public Class AUSUWs
Inherits System.Web.Services.WebService

<WebMethod()> _
<ScriptMethod(ResponseFormat:=ResponseFormat.Json)> _
Public Function getInventoryForWs() As String
    Try
        Dim a As New AUSUReport
        Dim dt As DataTable = a.getInventoryForWs().Tables(0)
        Return (GetJson(dt))
    Catch ex As Exception
        Throw ex
    End Try
End Function
Public Function GetJson(ByVal dt As DataTable) As String
    Dim serializer As New System.Web.Script.Serialization.JavaScriptSerializer()
    Dim rows As New List(Of Dictionary(Of String, Object))()
    Dim row As Dictionary(Of String, Object) = Nothing

    For Each dr As DataRow In dt.Rows
        row = New Dictionary(Of String, Object)()
        For Each col As DataColumn In dt.Columns
            row.Add(col.ColumnName.Trim(), dr(col))
        Next
        rows.Add(row)
    Next
    Return serializer.Serialize(rows)
End Function
End Class

谁能帮我这个?

4

1 回答 1

0

我认为这部分与双斜杠不正确:

 url: "http://somewebsite.com//
       DashboardUI/Reports/Marketing/AUSUWs.asmx/getInventoryForWs", 

尝试这个:

 url: "http://somewebsite.com/
       DashboardUI/Reports/Marketing/AUSUWs.asmx/getInventoryForWs", 

编辑:

“dataType 应该是您收到的类型,但 contentType 应该是您发送的 mime 类型,以下应该没问题”

JQuery.ajax({ 
    type: "POST", 
    url: "http://somewebsite.com/DashboardUI/Reports/Marketing/AUSUWs.asmx/getInventoryForWs",       
    dataType: "json", 
    success: function (ret) { 
              $(ret).find("Table").each(function () 
               { 
                   alert('success'); 
                   // $('#').val($(this).find('').text());
                });
        },
        error: function (httpRequest, textStatus, errorThrown) {
            alert("status=" + textStatus + ",error=" + errorThrown);
        }
    });

这是一个详细的演练。像这里一样做所有事情,你不会有问题: Introduction to using jQuery with Web Services (VB.Net)

于 2013-07-04T06:28:07.630 回答