asp.net - 从 jQuery 调用 ASP.NET ASMX Web 服务

标签 asp.net jquery json soap asmx

我尝试使用 JSON 从 jQuery 调用 ASMX 并获取 SOAP 错误响应。

我做错了什么?!

这是我的 jQuery 调用:

 $.ajax({
            type: "POST",
            url: '/Services/TeamPerson.asmx',
            contentType: "application/json; charset=utf-8",
            data: {'active':false, 'team_id':team_id, 'player_id':player_id},
            dataType: "json",
            success: function(msg) {
              alert(msg);
            },
            error: function(xhr, msg) { alert(msg + '\n' + xhr.responseText); }
          }); 

这是我的网络服务:

[WebService(Namespace = "http://tempuri.org/")]
  [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
  [ToolboxItem(false)]
  [ScriptService]
  public class TeamPerson : WebService
  {
    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public void SetPlayerStatus(bool active, ulong team_id, ulong player_id)
    {
      // blah blah
    }
  }

我收到了一个令人讨厌的 SOAP 错误:

<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body><soap:Fault><soap:Code>
    <soap:Value>soap:Receiver</soap:Value></soap:Code>
    <soap:Reason><soap:Text xml:lang="en">
     System.Web.Services.Protocols.SoapException: Server was unable to process request. ---&gt; 
     System.Xml.XmlException: Data at the root level is invalid. Line 1, position 1.   
   at System.Xml.XmlTextReaderImpl.Throw(Exception e)    
   at System.Xml.XmlTextReaderImpl.Throw(String res, String arg)    
   at System.Xml.XmlTextReaderImpl.ParseRootLevelWhitespace()    
   at System.Xml.XmlTextReaderImpl.ParseDocumentContent()    
   at System.Xml.XmlTextReaderImpl.Read()    
   at System.Xml.XmlTextReader.Read()   
   at System.Web.Services.Protocols.SoapServerProtocol.SoapEnvelopeReader.Read()    
   at System.Xml.XmlReader.MoveToContent()    
   at System.Web.Services.Protocols.SoapServerProtocol.SoapEnvelopeReader.MoveToContent()    
   at System.Web.Services.Protocols.SoapServerProtocolHelper.GetRequestElement()    
   at System.Web.Services.Protocols.Soap12ServerProtocolHelper.RouteRequest()    
   at System.Web.Services.Protocols.SoapServerProtocol.RouteRequest(SoapServerMessage message)    
   at System.Web.Services.Protocols.SoapServerProtocol.Initialize()
       at System.Web.Services.Protocols.ServerProtocol.SetContext(Type type, HttpContext context, HttpRequest request, HttpResponse response)

   at System.Web.Services.Protocols.ServerProtocolFactory.Create(Type type, HttpContext context, HttpRequest request, HttpResponse response, Boolean&amp; abortProcessing)

   --- End of inner exception stack trace ---</soap:Text></soap:Reason><soap:Detail /></soap:Fault></soap:Body></soap:Envelope>

我可以通过浏览器点击 /Services/TeamPerson.asmx 并且 Web 方法工作正常。我还在 web.config 中定义了以下内容:

<httpHandlers>
   <remove verb="*" path="*.asmx"/>
   <add verb="*" path="*.asmx" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
   <add verb="*" path="*_AppService.axd" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
   <add verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" validate="false"/>
      <add verb="GET,HEAD,POST" path="TinyMCEHandler.aspx" type="Moxiecode.TinyMCE.Web.HttpHandler,Moxiecode.TinyMCE" />
  </httpHandlers>
  <httpModules>
   <add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
  </httpModules>

最佳答案

您没有定义在服务上调用哪个方法。尝试将 JQuery 行 url: '/Services/TeamPerson.asmx', 更改为 url: '/Services/TeamPerson.asmx/SetPlayerStatus',

关于asp.net - 从 jQuery 调用 ASP.NET ASMX Web 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3506634/

相关文章:

c# - 如何使用 linq 树表达式创建具有动态列的数据透视表

c# - 如何在 WebAPI 的 Controller 中使用 DTO?

php - 将酒店菜单存储在数据库中

java - json对象包含转换为 "\n"的换行符,同时将json对象转换为字符串或字节

c# - 如何设置 JSON 对象从结果开始?.Net MySQL JSON Restful Webservice?

c# - 每个服务器端事务的加载程序

javascript - 滚动到 ASP.NET 列表框中第一个选定的项目

javascript - 在 Javascript 中重新格式化复杂文本日期字符串的更好方法

javascript - 转移到新目的地时未捕获 jQuery 事件值

jquery - 使用 jQuery 根据复选框 ID 填充文本字段?