doo*_*urt 3 c# jquery web-services asmx
我和ASMX Web服务无法使用.我们争吵.她提出了我们过去的论点.这是一种痛苦.我们的关系在岩石上!
我有一个ASMX Web服务,我没有使用Newtonsoft库进行序列化(如下所述:http://encosia.com/2011/04/13/asp-net-web-services-mistake-manual-json - 序列化/).它看起来像这样:
[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;
}
Run Code Online (Sandbox Code Playgroud)
够简单吧?它在搜索时返回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>
Run Code Online (Sandbox Code Playgroud)
我期待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']);
}
});
Run Code Online (Sandbox Code Playgroud)
我可以看到使用Chrome的通话:

然而一切都发生了!没有Jquery错误,没有烟花,没有任何东西.她无视我.
起初我是在责备web服务,但我认为这可能与我如何在jquery中解析和格式化数据有关.
所以,我的问题是,我做错了什么以及如何使自动完成正常工作?
帮助赞赏:)
编辑:它可能没有帮助,但这是我在提琴手中看到的:

jQuery UI自动完成不再使用formatItem方法.这和许多类似的方法,在自动完成的早期版本中,这是一个插件存在这里
我使用jQuery UI的自动完成重写了你的代码,它适用于我的下面的htm和asmx文件.
有关可以使用的更多方法,请参阅jQueryUI自动完成的演示.
我使用了www.json.org上的json2.min.js.我还将[System.Web.Script.Services.ScriptService]属性添加到Service1类中,以便可以直接从jQuery作为json Web服务调用它.
这些文章帮助了我:
在ASP.NET AJAX中使用jQuery时要避免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>
Run Code Online (Sandbox Code Playgroud)
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" };
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7873 次 |
| 最近记录: |