使用jquery使用wcf服务

时间:2014-04-23 13:58:24

标签: c# jquery json wcf cors

所以我正在尝试使用json来使用wcf服务。我想要它做的是从一个简单的html表单中获取值,并通过json将这些值分配给wcf中实例化的对象。由于我无法这样做,我试着尽可能地缩小它,所以你可以(也许)帮我解决这个问题。现在,只是为了知道它是否有效,我希望wcf服务能够回答一个ok字符串。

这是我的html / js代码

   function saveSection() {

        $.ajax({
            url: "http://localhost:52928/SectionService.svc/Guardar",
            data: '{"name": "' + sectionName.value + '", "title": "' + sectionTitle.value + '", "scope": "' + sectionScope.value + '",  "description": "' + sectionDescription.value + '"}',
            type: "GET",
            dataType: 'json',
            success: function () { alert('it works') },
            error: function () { alert('it doesnt work') },
            contentType: "application/json",

        });
    }

 </script>

这是我的服务

public class SectionService : ISection
{
    public string Guardar(string name, string title, string scope, string description)
    {
        return "OK";
    }

}

这是章节界面

namespace WcfSection
{
[ServiceContract]
public interface ISection
    {
      [WebInvoke(Method = "GET", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
      [OperationContract]
        string Guardar(string name, string title, string scope, string description);
    }
}

最后,我认为问题可能出在这里,web.config

<?xml version="1.0"?>
<configuration>
  <system.serviceModel>
     <bindings>      
       <webHttpBinding>
     <binding name="webHttpBindingWithJsonP" crossDomainScriptAccessEnabled="true" />
   </webHttpBinding>
 </bindings>
 <services>
   <service name="WcfSection.SectionService" behaviorConfiguration="MyServiceBehavior">
     <host>
       <baseAddresses>
         <add baseAddress="http://localhost:52928/"/>
       </baseAddresses>
     </host> 
     <endpoint address="" binding="webHttpBinding" bindingConfiguration="webHttpBindingWithJsonP" behaviorConfiguration="webHttpBehavior" contract="WcfSection.ISection"/>
   </service>
 </services>
 <behaviors>
   <endpointBehaviors>
      <behavior name="webHttpBehavior"  />
  </endpointBehaviors>
  <serviceBehaviors>
     <behavior name="MyServiceBehavior">
       <serviceMetadata httpGetEnabled="True"/>
     </behavior>
   </serviceBehaviors>
 </behaviors>
</system.serviceModel>
<system.webServer>
<httpProtocol>
<customHeaders>
  <add name="Access-Control-Allow-Origin" value="*" />
  <add name="Access-Control-Allow-Methods" value="GET,PUT,POST,DELETE,OPTIONS" />
  <add name="Access-Control-Allow-Headers" value="Content-Type" />
</customHeaders>
</httpProtocol>
</system.webServer>
<system.web>
  <webServices>
    <protocols>
        <add name="HttpGet"/>
        <add name="HttpPost"/>
    </protocols>
</webServices>
<compilation debug="true"/>
</system.web>
</configuration>

我想补充一点,这是我第一次尝试这样做,而且html和服务属于不同的项目,我努力解决CORS问题只是为了达到这一点。它现在所做的只是显示“它不起作用”警报。

1 个答案:

答案 0 :(得分:0)

我没有在我的电脑上试用你的代码。但似乎至少你需要添加到端点行为中。

<endpointBehaviors>
      <behavior name="webHttpBehavior">
          <webHttp />
      </behavior>
  </endpointBehaviors>