我在vs2012中創建了一個RESTful wcf服務。我可以訪問這個服務frfom網絡brovsers。我的接口文件是這樣:jquery和RESTful wcf服務
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");
}
}
});
我必須做什麼,我必須寫類型,URL,數據,contentype,數據類型等,請幫我已經工作了幾天
可以在任何請幫助我 –