我正在使用asp.net。我已經在C#中創建了一個WCF服務並託管在IIS服務器上。我在我的asp.net web應用程序中使用JQuery調用這個服務。當我使用JQuery調用該服務時,它將會出現錯誤功能,警報中會顯示空的消息。使用JQuery調用WCF服務時WCF服務調用失敗
調用服務表單.aspx頁面。
<script src="Script/jquery-1.10.2.min.js" type="text/javascript"></script>
<script type="text/javascript" language="javascript">
function callService()
{
var value = 10;
$.ajax({
type: "POST",
url: "http://localhost/IISWCF/Service1.svc/getdata",
contentType: "application/json; charset=utf-8",
data: '{"value": "' + value + '"}',
dataType: "json",
processdata: true, //True or False
error: function(XMLHttpRequest, textStatus, errorThrown) {
alert("error: " + errorThrown);
},
success: function(msg) {
alert(msg.d);
}
});
}
</script>
<script type="text/javascript" language="javascript">
$(document).ready(function(){
callService();
})
</script>
Service1.svc文件
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class Service1 : IService1
{
public string GetData(int value)
{
return string.Format("You entered: {0}", value);
}
}
IService1.cs文件
namespace WcfService1
{
[ServiceContract]
public interface IService1
{
[OperationContract]
[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json)]
string GetData(int value);
}
}
WCF的web.config文件
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="WcfService1.Service1Behavior">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="EndpBehavior">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
<services>
<service name="WcfService1.Service1" behaviorConfiguration="WcfService1.Service1Behavior">
<endpoint behaviorConfiguration="EndpBehavior" address="" binding="webHttpBinding" contract="WcfService1.IService1">
<identity>
<dns value="localhost"/>
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
</services>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
</system.serviceModel>
請幫我解決了這個問題。
嘗試使用此文章中提到的方向,因爲它似乎你可能會需要JSON作爲響應。 WebGet和WebInvoke似乎缺失。 http://www.codeproject.com/Articles/132809/Calling-WCF-Services-using-jQuery –
我已按照鏈接中提到的步驟進行操作,問題未解決。我用新的編輯過我的問題。你可以幫我嗎。 – user2322512
更改端點綁定到webHttpBinding – Erwin