为Web服务设置IIS

问题描述:

我无法获得任何教程或设置说明以允许Web服务被JavaScript函数使用。在我的电脑,浏览器只是抛出一个为Web服务设置IIS

500(内部服务器错误)

,并运行在服务器上的网页时,IE抛出

需要跨源资源共享( CORS)。

需要CORS预检。

我已经将所需的跨域设置添加到webconfig,但它仍然无法正常工作。

<configuration> 
    <system.web> 
     <customErrors mode="Off"/> 
     <compilation strict="false" explicit="true" targetFramework="4.5" /> 
     <httpRuntime targetFramework="4.5" /> 
     <webServices> 
     <!-- added because asmx --> 
     <protocols> 
      <add name="HttpGet"/> 
      <add name="HttpPost"/> 
     </protocols> 
     </webServices> 
    </system.web> 
    <system.webServer> 
    <httpProtocol> 
     <customHeaders> 
     <add name="Access-Control-Allow-Origin" value="*" /> 
     <add name="Access-Control-Allow-Headers" value="Content-Type" /> 
     <add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE, OPTIONS" /> 
     </customHeaders> 
    </httpProtocol> 
    </system.webServer> 

</configuration> 

的Javascript:

function GetMember() { 
    $.ajax({ 
     type: "POST", 
     url: "https://www.mywebsite.org/Callsickapp/Webservice1.asmx/HelloWorld", 
     contentType: "application/json; charset=utf-8", 
     dataType: "json", 
     success: OnGetMemberSuccess, 
     error: OnGetMemberError 
    }); 
} 
document.getElementById("btnCallService").onclick = GetMember; 
function OnGetMemberSuccess(data, status) { 
    //jQuery code will go here... 

    document.getElementById("answers").innerHTML= data.d; 
} 
function OnGetMemberError(request, status, error) { 
    //jQuery code will go here... 
    document.getElementById("answers").innerHTML= request.statusText 
} 

我缺少什么?

我只需要实际阅读由Visual Studio生成的Web服务.vb文件顶部的注释。

'To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 

' <System.Web.Script.Services.ScriptService()> _ 

简单的 “HelloWorld” Web服务,并从教程其他Web服务现在正常工作。