我正在学习 WCF 服务。我正在尝试从 Jquery 调用 RESTful 服务。我的服务如下
服务等级
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://localhost:2319/RESTFullService.svc/welcome/Mahesh”时,它返回的值为
"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>
我的编码有什么问题?请帮我...
谢谢
马赫什