我有一个简单的WCF服务,它返回JSON格式的字符串列表。 WCF服务代码如下所示:
String address = "";
for (TransactionInput txin : tx.getInputs()){
address = txin.getFromAddress().toString();
}
:
Web.config
<system.serviceModel>
<services>
<service name="WcfServiceProto.Service1"
behaviorConfiguration="ServiceBehavior1">
<endpoint
address=""
behaviorConfiguration="EndPointBehavior"
binding="webHttpBinding"
contract="WcfServiceProto.IService1" />
<!--<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>-->
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="ServiceBehavior1">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="EndPointBehavior">
<webHttp />
</behavior>
</endpointBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
:
Service1.cs
我正在尝试使用JQuery ajax调用此服务,如下所示:
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class Service1 : IService1
{
[WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.WrappedRequest)]
public List<string> AutoCompleteSearch(string query)
{
List<string> data = new List<string>(new string[] { "AB11", "AB12", "AB13", "BC11", "BC12", "BC13", "BC14", "CD11", "CD12", "CD13", "CD14", "CD15", "CD16", "CD17", "CD18", "CD19", "CD20", "CD21", "CD22", "CD23", "CD24", "CD25", "CD26", "CD27", "CD28", "CD29", "CD31", "CD32", "CD33", "CD34", "CD35", "CD36", "CD37", "CD38", "CD39", "CD41", "CD42", "CD43", "CD44", "CD45", "CD46", "CD47", "CD48", "CD49", "CD51", "CD52", "CD53", "CD54", "CD55", "CD56",});
List<string> results = data.Where(item => item.ToUpper().StartsWith(query.ToUpper())).ToList();
return results;
}
}
但是,当我在文本框中输入时,我看不到列表。我只看到带有文字的警报&#34;错误&#34;在它上面没有错误细节。我也在WCF服务中插入断点但它永远不会被击中。请告诉我这里我做错了什么。
浏览器控制台日志:
<script>
$(document).ready(function () {
//alert("Hey");
SearchText();
function SearchText() {
$("#Tags").autocomplete({
source: function (request, response) {
alert($('#Tags').val());
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "http://localhost:59227/Service1.svc/AutoCompleteSearch",
data: JSON.stringify({ query: $('#Tags').val() }),
dataType: "json",
dataFilter: function (data) { return data; },
success: function (data) {
alert("called");
response($.map(data, function (item) {
return { value: item };
}));
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert(textStatus);
}
});
}
});
}
});
</script>
答案 0 :(得分:2)
在您的Operational Contract(接口IService1中的方法)上提供WebInvoke属性。同时尝试设置UriTemplate也是如此。 IMO,理想情况下应该是GET请求,而不是POST。
[WebInvoke(Method=“GET”, ResponseFormat=WebMessageFormat.Json, BodyStyle=WebMessageBodyStyle.Wrapped, UriTemplate=“AutoCompleteSearch?query={query}” )]
修改服务层后,更新$ .ajax以使用GET作为类型/使用$ .get()并将参数作为查询字符串传递,名称为“query”。
function SearchText() {
$("#Tags").autocomplete({
source: function (request, response) {
alert($('#Tags').val());
var url = "http://localhost:59227/Service1.svc/AutoCompleteSearch?query="+$('#Tags').val();
$.get(url)
.done(function(data){ // your success code goes here})
.fail(function(ex){ // you failure code goes here});
}
});
}
我没有测试$ .get方法,如果您有任何问题,请查看此处 - https://api.jquery.com/jquery.get/