我已经在一个应用程序中创建了一个web服务。现在我需要在我的另一个网站中使用它。我知道如果webservice在同一个应用程序中,如提供像“webservicename.asmx / getInventoryForWs”这样的URL,它是如何完成的使用jquery。 我的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
任何人都可以帮我这个吗?
答案 0 :(得分: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)