0
我有2個項目(WCF項目和Asp.Net項目)。我遵循這個教程here。Web服務WCF和Javascript
在本教程中,我們將在一個項目中實現所有功能(請參閱源here)。
我在Web項目中引用了我的WCF項目。
我嘗試在我的警報中顯示一個值,但沒有任何值。
這裏是我的Service1.svc:
namespace WSSage100{
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class Service1 : IService1
{
public string GetData(int value)
{
return string.Format("You entered: {0}", value);
}
public string GetData2(int value)
{
return string.Format("You entered: {0}", value);
}
public string[] GetUser(string Id)
{
return new User().GetUser(Convert.ToInt32(Id));
}
public string GetTEST()
{
return "OKKKKKKKK";
}
}
這裏是IService1.cs:
namespace WSSage100
{
[XmlSerializerFormat]
[ServiceContract(Namespace ="WSSage100")]
public interface IService1
{
[OperationContract]
[WebInvoke(Method = "GET",ResponseFormat = WebMessageFormat.Json)]
string GetData(int value);
[OperationContract]
[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json)]
string GetData2(int value);
[OperationContract]
[WebInvoke(Method = "POST",BodyStyle = WebMessageBodyStyle.Wrapped,ResponseFormat = WebMessageFormat.Json)]
string[] GetUser(string Id);
[OperationContract]
[WebInvoke(Method = "GET",ResponseFormat = WebMessageFormat.Json)]
string GetTEST();
這裏是我的網頁(CSS)與JavaScript代碼:
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script language="javascript" type="text/javascript" src="JavaScript/jquery-1.10.2.min.js"></script>
<script type="text/javascript">
var Type;
var Url;
var Data;
var ContentType;
var DataType;
var ProcessData;
function WCFJSON() {
var userid = "1";
Type = "POST";
Url = "Service1.svc/GetUser";
Data = '{"Id": "' + userid + '"}';
ContentType = "application/json; charset=utf-8";
DataType = "json"; varProcessData = true;
CallService();
}
function CallService() {
$.ajax({
type: Type, //GET or POST or PUT or DELETE verb
url: Url, // Location of the service
data: Data, //Data sent to server
contentType: ContentType, // content type sent to server
dataType: DataType, //Expected data format from server
processdata: ProcessData, //True or False
success: function (msg) {//On Successfull service call
ServiceSucceeded(msg);
},
error: ServiceFailed// When Service call fails
});
}
function ServiceFailed(result) {
alert('Service call failed: ' + result.status + '' + result.statusText);
Type = null;
varUrl = null;
Data = null;
ContentType = null;
DataType = null;
ProcessData = null;
}
function ServiceSucceeded(result) {
if (DataType == "json") {
resultObject = result.GetUserResult;
for (i = 0; i < resultObject.length; i++) {
alert(resultObject[i]);
}
}
}
function ServiceFailed(xhr) {
alert(xhr.responseText);
if (xhr.responseText) {
var err = xhr.responseText;
if (err)
error(err);
else
error({ Message: "Unknown server error." })
}
return;
}
$(document).ready(
function() {
WCFJSON();
}
);
</script>
<style type="text/css">
#form1 {
height: 255px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div></div>
</form>
</body>
</html>
這裏是我的Web項目中的Service1.svc:
<%@ ServiceHost Language="C#" Debug="true" Service="WSSage100.Service1"%>
這是我在我的Web項目的web.config:
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" />
</system.web>
<system.serviceModel>
<client>
<endpoint address="http://localhost:52768/Service1.svc" binding="basicHttpBinding"
bindingConfiguration="BasicHttpBinding_IService1" contract="WebServiceSage100.IService1"
name="BasicHttpBinding_IService1" />
</client>
<behaviors>
<serviceBehaviors>
<behavior name="">
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="EndpBehavior">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"
multipleSiteBindingsEnabled="true" />
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_IService1" />
</basicHttpBinding>
</bindings>
<services>
<service name="WSSage100.Service1">
<endpoint address="" binding="webHttpBinding" contract="WSSage100.IService1">
</endpoint>
</service>
</services>
</system.serviceModel>
</configuration>
布萊恩爲localhost:52768/Service1.svc顯示
當我使用檢查元素我有這樣的錯誤
我是這個領域的新手,對不起我的英文不好。
在控制檯中的任何錯誤? – iJade
我有任何錯誤。 – Jayce
如果您在瀏覽器地址欄中輸入http://localhost:52768/Service1.svc,您會得到什麼迴應? – Brian