【问题标题】:Setting up IIS for Web Service为 Web 服务设置 IIS
【发布时间】:2017-06-16 18:23:18
【问题描述】:

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

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
}

我错过了什么?

【问题讨论】:

    标签: javascript vb.net iis-7


    【解决方案1】:

    我只需要实际阅读 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”网络服务和教程中的其他网络服务现在可以正常工作了。

    【讨论】:

      猜你喜欢
      • 2015-12-23
      • 1970-01-01
      • 1970-01-01
      • 2018-04-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-27
      • 1970-01-01
      相关资源
      最近更新 更多