【问题标题】:WebService with JQuery使用 JQuery 的 Web 服务
【发布时间】:2014-03-31 08:19:18
【问题描述】:

我正在开发我的第一个 jQuery 和 ASP.Net 网络服务,但它无法运行,希望有人能告诉我我的方式的错误吗?

我设置了一个 jsFiddle:http://jsfiddle.net/3hufY/1/,在 1.10.1 上有基本的 HTML 和 jQuery

下面的 asmx 文件代码(抱歉在 VB 中)

Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.ComponentModel

' To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment 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 myService
Inherits System.Web.Services.WebService

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

<WebMethod()> _
Public Function FullName(ByRef First As String, ByRef Last As String) As String
    Return "Your name is " & First & " " & Last
End Function

End Class

在小提琴中显示完整的 URL,在本地我只显示本地 URL,以防万一它有所作为。

【问题讨论】:

  • 您能告诉我们您的具体问题是什么吗?
  • 我没有得到响应,而是返回错误

标签: javascript jquery asp.net vb.net web-services


【解决方案1】:

您应该启用脚本中的ScriptService 部分,该部分现在已被注释掉。

根据MSDN

要从 ECMAScript (JavaScript) 调用 Web 服务方法,您必须将 ScriptServiceAttribute 属性应用于相关的 Web 服务类

我确实建议您在开始一个新项目时研究 WCF 或 Web API,因为 ASMX Web 服务已报废。这些框架非常易于使用,尤其是对于这些场景。

【讨论】:

  • @MarkCooney:除非您使用 SOAP,否则它将无法工作,因为它比 JSON 更难阅读。你真的应该使用另一个平台。见WebAPI
  • 我似乎没有 WebAPI 作为我的选项。我在服务器 2008 操作系统上使用 VS2010
  • @MarkCooney:见post。它也应该适用于 Server 2008。
  • 一如既往的教程对 VB 不太友好,所以现在比今天早上更困惑。我只是一个爱好,所以学习语言并不是我真正有时间做的事情。微软要么需要支持 VB.Net,要么放弃。无论如何感谢您的帮助@patrick_hofman
【解决方案2】:

对您的网络服务进行以下更改

Imports System.Web.Script.Services

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

<WebMethod()> _
<ScriptMethod()> _
Public Function FullName(ByRef First As String, ByRef Last As String) As String
    Return "Your name is " & First & " " & Last
End Function

检查您是否放置或 如果您没有 ScriptManager , Importing Script.Service 将显示错误

【讨论】:

    猜你喜欢
    • 2018-05-19
    • 2012-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-19
    相关资源
    最近更新 更多