我正在學習WCF服務。我正試圖從Jquery調用RESTful服務。我的服務是如下使用JQuery Ajax調用WCF REST服務不起作用
服務類
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.ServiceModel.Activation;
namespace RESTfulServiceLib
{
[AspNetCompatibilityRequirements(RequirementsMode
= AspNetCompatibilityRequirementsMode.Allowed)]
public class RestFullService : IRestFullService
{
public string Welcome(string Name)
{
return "Welcome to Restful Service " + Name;
}
}
}
接口
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.ServiceModel.Activation;
namespace RESTfulServiceLib
{
[ServiceContract]
public interface IRestFullService
{
[OperationContract]
[WebInvoke(UriTemplate="/Welcome/{Name}",Method="GET",ResponseFormat=WebMessageFormat.Json)]
string Welcome(string Name);
}
}
我創建了一個服務主機和SVC文件是這樣的
<%@ ServiceHost Language="C#" Debug="true" Service="RESTfulServiceLib.RestFullService" Factory="System.ServiceModel.Activation.WebServiceHostFactory" %>
我有以下配置設置
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<behaviors>
<endpointBehaviors>
<behavior name="EndPBhvr">
<webHttp helpEnabled="true" defaultOutgoingResponseFormat="Json"
faultExceptionEnabled="true" />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="SvcBhvr">
<serviceMetadata httpGetEnabled="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
<services>
<service name="RESTfulServiceLib.RestFullService">
<endpoint address="" behaviorConfiguration="EndPBhvr"
binding="webHttpBinding" bindingConfiguration="" name="EP1"
contract="RESTfulServiceLib.IRestFullService" />
</service>
</services>
</system.serviceModel>
</configuration>
運行的應用程序,當我瀏覽網址後的 「http://本地主機:2319/RESTFullService.svc /首頁/馬赫什」 它返回的值作爲
"Welcome to Restful Service Mahesh"
我曾嘗試使用Jquery調用此服務。但我正在逐漸
error 200 undefined
的腳本如下
<!DOCTYPE html>
<html>
<head>
<script src="jquery-1.6.4.min.js"></script>
<script >
function ajaxcall()
{
$.ajax({
url: "http://localhost:2319/RESTFullService.svc/welcome/mahesh",
type: "GET",
contentType: "application/json; charset=utf-8",
dataType:"jsonp",
data:{},
processdata : true,
success: function(response)
{
var data= response.d;
alert(data);
},
error: function(e)
{
alert('error '+e.status + ' ' + e.responseText);
}
});
}
$().ready(function(){
$('#btntest').click(ajaxcall);
})
</script>
</head>
<body>
<button id="btntest" >Click Me</button>
</body>
</html>
什麼是錯誤的,我的編碼?請幫我...
感謝
馬赫什
我已經加入「警戒工作(XMLHttpRequest.responseText) ;」和'alert(e.statusText)';'除了現在的錯誤部分,並嘗試。現在e.statusText顯示'成功'。這是什麼意思? –