问题描述
||
我和ASMX Web服务无法正常运行。我们争吵。她提出了我们过去的观点。这很痛苦。我们的关系在石头上!
我有一个ASMX Web服务,但尚未与Newtonsoft库进行序列化(如此处所说明的原因:http://encosia.com/2011/04/13/asp-net-web-services-mistake-manual- json-serialization /)。看起来像这样:
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string[] GetCitiesWithState(string isoalpha2,string prefixText)
{
var dict = AtomicCore.CityObject.GetCitiesInCountryWithStateAutocomplete(isoalpha2,prefixText);
string[] cities = dict.Values.ToArray();
return cities;
}
很简单吧?搜索new
时返回:
<?xml version=\"1.0\" encoding=\"utf-8\"?>
<arrayofstring xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns=\"http://tempuri.org/\">
<string>New Orleans,Louisiana</string>
<string>New York,New York</string>
</arrayofstring>
我原本希望使用JSON,但是经过一番阅读之后,看来我不应该尝试对输出进行序列化-应该就可以了吗?无论如何,所以我在前端有以下JQuery:
$(\'#<%=txtCity.ClientID%>\').autocomplete(\'<%=ResolveUrl(\"~/AtomicService/Assets.asmx/GetCitiesWithState\")%>\',{
dataType: \'json\',httpMethod: \'POST\',contentType: \'application/json; charset=utf-8\',parse: function (data) {
var rows = new Array();
for (var i = 0; i < data.d.length; i++) {
rows[i] = { data: data.d[i],value: data.d[i].Value,result: data.d[i].Value };
}
return rows;
},formatItem: function (row,i,n) {
return row.Value;
},extraParams: {
minChars: 2,isoalpha2: \'<%=Session[\"BusinessCountry\"].ToString()%>\',maxRows: 20,prefixText: function () {
return $(\'#<%=txtCity.ClientID%>\').val()
}
},max: 20
}).result(function (event,data,formatted) {
if (data) {
alert(data[\'Key\']);
}
});
我可以使用Chrome查看通话情况:
然而,一切都发生了!没有Jquery错误,没有烟花,没有任何东西。她无视我。
起初,我指责Web服务,但是我认为这可能与我在jquery中解析和格式化数据的方式有关。
所以,我的问题是,我在做什么错?如何使自动完成功能正常工作?
感谢帮助:)
编辑:可能没有帮助,但这是我在fiddler中看到的内容:
解决方法
jQuery UI自动完成功能不再使用formatItem方法。 autocomplete的早期版本中提供了该方法以及许多其他此类方法,该方法是此处的插件
我已经使用jQuery UI的自动完成功能重写了您的代码,它对下面的htm和asmx文件有效。
请参阅有关jQueryUI自动完成功能的演示,以获取更多可以使用的方法。
我使用了来自www.json.org的json2.min.js
另外,我还向Service1类添加了[System.Web.Script.Services.ScriptService]属性,以便可以将它作为JSON Web服务直接从jQuery调用。
这些文章对我有帮助:
ASMX和JSON –常见的错误和误解
使用jQuery消耗ASP.NET JSON Web服务
将jQuery与ASP.NET AJAX结合使用时要避免的3个错误
htm文件
<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"
\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">
<html xmlns=\"http://www.w3.org/1999/xhtml\">
<head>
<title>jQuery to ASMX</title>
<link rel=\"Stylesheet\" type=\"text/css\"
href=\"http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.11/themes/cupertino/jquery-ui.css\"/>
<script type=\"text/javascript\" src=\"https://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js\"></script>
<script type=\"text/javascript\"
src=\"https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.11/jquery-ui.min.js\"></script>
<script type=\"text/javascript\" src=\"http://localhost/Scripts/json2.min.js\"></script>
</head>
<body>
<script type=\"text/javascript\">
$(document).ready(function() {
$(\"#txtInput\").autocomplete({
source:function (request,response) {
var paramters = {
isoalpha2: \'<%=Session[\"BusinessCountry\"].ToString()%>\',prefixText: request.term
};
$.ajax({
url: \'Service1.asmx/GetCitiesWithState\',type: \'POST\',dataType: \'json\',contentType: \'application/json; charset=utf-8\',data: JSON.stringify(paramters),success: function(data) {
response($.each(data.d,function(index,value) {
return {
label: value,value: index
}
}));
}
});
},minLength:2,delay:500
});
});
</script>
<p>
Hello,World!</p>
<label for=\"txtInput\">
Enter the string here:</label><input type=\"text\" id=\"txtInput\"/>
</body>
</html>
asmx文件
using System.Web.Script.Services;
using System.Web.Services;
namespace jQueryAutoCompleteBackEnd
{
/// <summary>
/// Summary description for Service1
/// </summary>
[WebService(Namespace = \"http://tempuri.org/\")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
[ScriptService]
public class Service1 : WebService
{
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string[] GetCitiesWithState(string isoalpha2,string prefixText)
{
return new string[] { \"New Orleans,Lousiana\",\"New York,New York\" };
}
}
}
,asmx网络方法返回XML而不是JSON的原因是因为HTTP方法是GET而不是POST。
为了使自动完成插件能够使用POST,您必须使用回调函数来实现source参数,请参见此处