首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >方法不对WCF服务进行ajax调用

方法不对WCF服务进行ajax调用
EN

Stack Overflow用户
提问于 2016-08-18 06:41:02
回答 1查看 524关注 0票数 0

我用WCF方法发送一个简单的ajax调用,当它返回bad-request但没有方法时,它显示的是成功的状态。

Ajax调用

代码语言:javascript
复制
$(document).ready(function(){
            $.get("http://localhost:1347/Service1.svc/DoWork", //this shows bad-request
          //$.get("http://localhost:1347/Service1.svc", //this shows success
function(data, status){

                alert("Data: " + data + "\nStatus: " + status);
        });

经营合同

代码语言:javascript
复制
namespace WcfService1
{
    [ServiceContract]
    public interface IService1
    {
        [OperationContract]
        string DoWork();
    }
}

Implementation

代码语言:javascript
复制
public class Service1 : IService1
    {

        public string DoWork()
        {
            return string.Format("This is DoWork()");
        }
    }

Global.asax

代码语言:javascript
复制
protected void Application_BeginRequest(object sender, EventArgs e)
        {
            HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*");
            if (HttpContext.Current.Request.HttpMethod == "OPTIONS")
            {
                HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST");
                HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept");
                HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "1728000");
                HttpContext.Current.Response.End();
            }
        }

Web.Config (编辑)

代码语言:javascript
复制
<?xml version="1.0"?>
<configuration>

  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true"/>
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.5.2"/>
    <httpRuntime targetFramework="4.5.2"/>
    <httpModules>
      <add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web"/>
    </httpModules>
  </system.web>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <!-- To avoid disclosing metadata information, set the values below to false before deployment -->
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <protocolMapping>
        <add binding="basicHttpsBinding" scheme="https"/>
    </protocolMapping>    
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true">
      <remove name="ApplicationInsightsWebTracking"/>
      <add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web"
        preCondition="managedHandler"/>
    </modules>


    <!--
        To browse web app root directory during debugging, set the value below to true.
        Set to false before deployment to avoid disclosing web app folder information.
      -->
    <directoryBrowse enabled="true"/>
    <validation validateIntegratedModeConfiguration="false"/>




  </system.webServer>

</configuration>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-08-18 06:49:36

首先,确保在合同中启用GET。

代码语言:javascript
复制
namespace WcfService1
{
    [ServiceContract]
    public interface IService1
    {
        [OperationContract]
        [WebGet(UriTemplate = "DoWork", ResponseFormat = WebMessageFormat.Json)]
        string DoWork();
    }
}

然后确保在配置中添加webHttp

代码语言:javascript
复制
  <endpointBehaviors>
    <behavior>
      <webHttp />
    </behavior>
  </endpointBehaviors>

您还可能需要设置绑定到webHttpBindig

我通常定义我的端点,但是我认为没有它您可以管理。这应该是一个完整的例子。

代码语言:javascript
复制
  <system.serviceModel>
    <services>
      <service name="WcfService1.IService1" behaviorConfiguration="myServiceBehavior">
        <endpoint address="" binding="webHttpBinding" behaviorConfiguration="myWebBehavior" contract="WcfService1.IService1" />
      </service>
    </services>
    <behaviors>
      <endpointBehaviors>
        <behavior name="myWebBehavior">
          <webHttp />
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior name="myServiceBehavior">
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <protocolMapping>
      <add binding="basicHttpsBinding" scheme="https"/>
    </protocolMapping>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>
  </system.serviceModel>

这里有一个完整的例子。

http://www.codeproject.com/Articles/105273/Create-RESTful-WCF-Service-API-Step-By-Step-Guide

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39011474

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档