2012-08-16 122 views
7

我有jquery調用帶有整數的asp.net webservice。 在我們已經移植到.net 4.0的學習應用程序中,我無法讓這個調用工作。 我可以調用它沒有參數,但將數據發送到Web方法返回以下錯誤的方法:jQuery Ajax到asp.net asmx web服務拋出請求格式無效:application/json

System.InvalidOperationException: Request format is invalid: application/json; charset=UTF-8. 
at System.Web.Services.Protocols.HttpServerProtocol.ReadParameters() 
at System.Web.Services.Protocols.WebServiceHandler.CoreProcessRequest() 

我在一個空白的項目創建完全相同的代碼,它工作得很好。我無法看到任何web.config中的空白項目添加,將有所作爲。

jQuery代碼

$.ajax({ 
    type: "POST", 
    url: "/WebService1.asmx/Test", 
    data: JSON.stringify({"code": 1234}), 
    contentType: "application/json; charset=utf-8", 
    dataType: "json", 
    success: function (msg) { 
     alert(msg); 
    } 
}); 

我的Web服務代碼

<ScriptService()> _ 
<WebService(Namespace:="http://tempuri.org/")> _ 
<WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _ 
<ToolboxItem(False)> _ 
Public Class WebService1 
    Inherits WebService 

    <WebMethod()> 
    Public Function Test(ByVal code As Integer) As String 
     Return "success" 
    End Function 

    <WebMethod()> 
    Public Function Hello() As String 
     Return "hello" 
    End Function  
End Class 

Web配置

<?xml version="1.0" encoding="UTF-8"?> 
<configuration> 
    <appSettings> 
    </appSettings> 
    <connectionStrings> 
    </connectionStrings> 
    <system.web> 
     <httpRuntime enableVersionHeader="false" /> 
     <httpCookies httpOnlyCookies="true" requireSSL="false" lockItem="true" /> 
     <trace enabled="false" pageOutput="true" requestLimit="40" localOnly="true"/> 

     <httpModules> 
     </httpModules> 
     <compilation debug="true" strict="true" explicit="true" targetFramework="4.0"> 

     </compilation> 
     <pages controlRenderingCompatibilityVersion="3.5" clientIDMode="AutoID"> 
     </pages> 

     <authentication mode="Forms"> 

     <httpHandlers>  
     </httpHandlers> 
    </system.web> 

    <system.webServer> 

    <validation validateIntegratedModeConfiguration="false" /> 
    <modules> 
    </modules> 
    <handlers> 
    </handlers> 
    <httpErrors errorMode="Custom" > 
    </httpErrors> 
    </system.webServer> 
</configuration> 
+0

你可以發佈你的web.config文件嗎?我知道問題出在哪裏,只需要確認,然後再發布答案。 – 2012-08-16 17:15:36

+0

@DarinDimitrov我已經添加了web.config,但刪除了敏感的細節 – skyfoot 2012-08-17 08:28:18

回答

10

DOH,我錯了web.config文件在工作。

就像很多問題一樣,解決方案是添加以下內容。

<system.webServer> 
     <handlers> 
      <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" /> 
     </handlers> 
    </system.webServer> 
+0

謝謝。解決了我的問題。但你能解釋一下它的功能嗎?在我的情況下,它是特定於一臺機器。任何想法可能是什麼原因?如果我不想更改我的配置文件,可以解決這個問題,因爲它可以在其他機器上正常工作。 – Akie 2014-03-21 14:41:05

+0

很高興你在錯誤的web.config中工作併發布了問題和答案。這節省了我很多時間。 – 2014-08-26 17:57:28

+0

@Akie我也不確定,但我可以告訴你的是ScriptHandlerFactory會在編譯時將您的WebServices添加到JavaScript代碼中作爲引用。這意味着您可以通過JavaScript中的語法'namespace.class.method'直接調用WebServices中的WebMethods。但是,這是我困惑的地方,所以我不能給出任何答案。 – 2017-01-27 17:58:43

9

我有同樣的問題,並結束了運行此命令

C:\Windows\Microsoft.NET\Framework64\v4.0.30319>aspnet_regiis.exe -i 

請注意,您將需要在管理員模式下的命令提示符爲它工作。