Web服务从AJAX中抛出500错误,但在直接调用时则不会

时间:2013-05-07 14:40:25

标签: jquery .net ajax web-services

我有一个ASP.net应用程序正在对一个生活在同一目录中的VB.net web服务进行jQuery AJAX调用。在我们的测试盒上,一切都很美妙。在制作中,如果我转到webservice的测试表单,我可以检索数据。但是,AJAX调用会导致500错误。这是AJAX代码:

$.ajax({
    type: "POST",
    url: "webservice.asmx/Action",
    data: "{'SearchString':'" + request.term + "'}",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function (res) {
        alert("Success");            
    },
    error: function () {
    alert("Fail");
    }
});

我试过了:

  1. 摆弄该调用中数据参数的格式
  2. 设置webservice的响应格式([ScriptMethod(ResponseFormat = ResponseFormat.Json)])
  3. 将此添加到Web.Config:
  4. <webServices>
         <protocols>
             <add name="HttpSoap" />
             <add name="HttpGet" />
             <add name="HttpPost" />
          </protocols>
    </webServices>
    

    一切都是徒劳的。仅在从此功能调用时才会失败,并且仅在我们的生产箱中调用。如果我直接从.asmx页面的表单加载动作,它工作正常。

    我的想法是我们的​​测试服务器和生产服务器之间必须存在配置差异,但我找不到任何后果。此外,当服务器以相同的方式处理它们时,什么样的配置会导致它在被单向调用而不是另一种方式时不起作用?

    有没有人遇到过他们已经解决过的类似问题?

2 个答案:

答案 0 :(得分:0)

我能够将错误追踪到:

Request format is invalid: application/json; charset=UTF-8

一些研究让我在另一个StackOverflow问题上得到了这个答案:the correct answer

一切都很好。

答案 1 :(得分:0)

请查看以下内容:

  1. <webServices>部分(位于<system.web>部分内)中添加以下内容:

    <protocols>
        <add name="HttpGet"/><add name="HttpPost"/>
    </protocols>
    

  2. 将以下内容添加到您的web.config的<handlers>部分(<system.webServer>部分的<configuration>内)中(如this论坛中所述):

     <add name="ScriptHandlerFactory" verb="*" path="*.asmx" 
     preCondition="integratedMode" 
     type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions,  
     Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    
     <add name="ScriptHandlerFactoryAppServices" verb="*" path="*_AppService.axd" 
     preCondition="integratedMode" 
     type="System.Web.Script.Services.ScriptHandlerFactory, 
     System.Web.Extensions, Version=3.5.0.0, Culture=neutral, 
     PublicKeyToken=31BF3856AD364E35" />
    
  3. 就我而言,这解决了这个问题。我不得不添加步骤1.和2.单独一个没有帮助。