我在vs2012中创建了一个RESTful wcf服务。我可以访问这个sservice frfom web brovsers。我的接口文件是这样的:
namespace RestService
{
[ServiceContract]
public interface IRestServiceImpl
{
[OperationContract]
[WebInvoke(Method = "GET",
ResponseFormat = WebMessageFormat.Xml,
BodyStyle=WebMessageBodyStyle.Wrapped,
UriTemplate = "xml/{id}"
)]
string XMLData(string id);
[OperationContract]
[WebInvoke(Method = "GET",
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Wrapped,
UriTemplate = "json/{id}"
)]
string JSONData(string id);
}
}
我的配置文件是:
<system.serviceModel>
<services>
<service name="RestService.RestServiceImpl" behaviorConfiguration="ServiceBehavior">
<endpoint address="" binding="webHttpBinding" contract="RestService.IRestServiceImpl" behaviorConfiguration="web">
</endpoint>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="ServiceBehavior">
<!-- 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>
<endpointBehaviors>
<behavior name="web">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
<protocolMapping>
<add binding="basicHttpsBinding" scheme="https" />
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
</system.serviceModel>
当我使用ctr + F5运行我的服务时,它会向我提供该网址localhost:50046/RestServiceImpl.svc
当我向网络浏览器写入代码时,我可以访问我的服务并得到响应:
代码为//localhost:50046/RestServiceImpl.svc/json/123
回复是{"JSONDataResult":"you requested product 123"}
现在我的问题是,我将从我的HTML代码访问该服务。我将发送数据进行服务并获得该服务的响应。我的简单jquery代码是:
$("input:#giris").click(function(){
function GetCategoriesJson() {
$.ajax(
{
type:"GET",
url:"localhost:50046/RestServiceImpl.svc",
data:"{123}",
contentType: "application/json; charset=utf-8",
dataType: "json/{123}",
success: OnSuccessJson,
error: OnErrorJson
});
function OnSuccessJson(data, status) {
alert("basarili")
}
function OnErrorJson(data, status) {
alert("Exception");
}
}
});
我必须做什么,我必须写什么类型,网址,数据,内容类型,数据类型等。请帮助我已经工作了几天
答案 0 :(得分:0)
从客户端调用时会出现什么错误?
因为它是WCF,我猜它是配置问题,请在.config文件中尝试。
<standardEndpoint name="" helpEnabled="true"
automaticFormatSelectionEnabled="false"
defaultOutgoingResponseFormat ="Json" />
以下是一个很好的例子:http://www.codeproject.com/Articles/128478/Consuming-WCF-REST-Services-Using-jQuery-AJAX-Call
答案 1 :(得分:0)
我不确定但是你可以试试这个: -
在ajax调用更改URL到此
url:"localhost:50046/RestServiceImpl.svc/json/123"