我今天阅读了无数关于创建 WCF 服务的演练,在尝试了太多次之后,我已经到了这一点,不得不放弃这一天以保持理智。我正在尝试测试 API(SmartyStreets),检索他们在其网站上列出的测试地址并以 JSON 格式返回响应。
我有一个 ISmartySreets.cs:
[ServiceContract]
public interface ISmartyStreetsService
{
[OperationContract]
[WebInvoke(Method = "GET", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest)]
string AddressLookup(string learnersID, string street, string secondary, string city, string state, string zip);
}
我有一个 SmartyStreets.svc.cs 文件:
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class SmartyStreetsService : ISmartyStreetsService
{
public string AddressLookup(string street, string secondary, string city, string state, string zip)
{
//Lookup returns JSON of addresses, or string error.
return Lookup(street, secondary, city, state, zip);
}
}
我有一个 Frankenstein web.config,它是我研究中不断编辑的受害者:
<?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"/>
<customErrors mode="Off"/>
</system.web>
<system.serviceModel>
<services>
<service name="SmartyStreetsWCF.SmartyStreetsService" behaviorConfiguration="serviceBehavior">
<endpoint address=""
binding="webHttpBinding"
contract="SmartyStreetsWCF.ISmartyStreetsService"
behaviorConfiguration="web"></endpoint>
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="web">
<webHttp />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="serviceBehavior">
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
<behavior name="">
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
<protocolMapping>
<add binding="webHttpBinding" scheme="https" />
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*"/>
<add name="Access-Control-Allow-Headers" value="Content-Type, Accept" />
<add name="Access-Control-Allow-Methods" value="POST,GET,OPTIONS" />
<add name="Access-Control-Max-Age" value="1728000" />
</customHeaders>
</httpProtocol>
<modules runAllManagedModulesForAllRequests="true"/>
<directoryBrowse enabled="true"/>
</system.webServer>
</configuration>
最后,我有一个非常普通的 HTML 文件,使用 Jquery 调用我的服务,托管在非本地 Web 服务器上。
$.ajax({
url: "http://wcftesting2.com/SmartyStreets.svc/AddressLookup",
type: "GET",
data: {street: "1 Rosedale St", secondary: "", city: "Baltimore", state:"MD", zip:"21229"},
contentType: "application/json",
dataType: "json",
success: function(addresses) {
alert("EUREKA!");
},
error: function(error) {
alert("FAILURE");
}
});
尝试从我的 HTML 页面运行 jquery 我得到以下信息: 预检响应具有无效的 HTTP 状态代码 405
“选项”如下:/SmartyStreets.svc/AddressLookup?street=1+Rosedale+St&secondary=&city=Baltimore&state=MD&zip=212
导航到链接:http ://wcftesting2.com/SmartyStreets.svc/AddressLookup?learnersID=12345&street=1+Rosedale+St&secondary=&city=Baltimore&state=MD&zip=21229 只是说,“方法不允许”。
我认为我的问题出在 web.config 文件和我如何使用 Jquery 调用服务之间,但我需要停止工作一个晚上。
如果有人能以最轻微的方式帮助我,那就太棒了。