我按照这篇文章中的说明进行操作:how can we integrate jquery autocomplete using asp.net, webservie and sql database
使jquery-autocomplete插件可以在asp.net上使用我的webservice,但是它不起作用。
如果我执行本地数据,则自动完成功能正常。我也可以使用jquery ajax调用我的web服务。谁能告诉我我做错了什么?代码:
//This jquery ajax call works perfectly
function AjaxTest() {
$.ajax({
type: "POST",
url: "/Services/OSServices.asmx/HelloWorld",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: RenderTest
});
}
//this autocomplete ajax call doesn't work. It doesn't even hit the breakpoint in the
web service
$("#lastName").autocomplete("/Services/OSServices.asmx/HelloWorld", {
parse: function(data) {
var parsed = [];
$(data).find("string").each(function() {
parsed[parsed.length] = {
data: [$(this).text()],
value: $(this).text(),
result: [$(this).text()]
};
});
return parsed;
},
datatype: "xml"
});
//this is the webservice code:
[WebService(Namespace = "http://tempuri.org")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
[System.Web.Script.Services.ScriptService]
public class OSServices : System.Web.Services.WebService
{
[WebMethod]
public string[] HelloWorld()
{
string[] myArray = { "blah1", "blah2", "blah3" };
return myArray;
}
}
编辑:
这是我得到的标题为内部服务器错误的响应。
<html>
<head>
<title>Request format is unrecognized for URL unexpectedly ending in '/HelloWorld'.</title>
<style>
body {font-family:"Verdana";font-weight:normal;font-size: .7em;color:black;}
p {font-family:"Verdana";font-weight:normal;color:black;margin-top: -5px}
b {font-family:"Verdana";font-weight:bold;color:black;margin-top: -5px}
H1 { font-family:"Verdana";font-weight:normal;font-size:18pt;color:red }
H2 { font-family:"Verdana";font-weight:normal;font-size:14pt;color:maroon }
pre {font-family:"Lucida Console";font-size: .9em}
.marker {font-weight: bold; color: black;text-decoration: none;}
.version {color: gray;}
.error {margin-bottom: 10px;}
.expandable { text-decoration:underline; font-weight:bold; color:navy; cursor:hand; }
</style>
</head>
<body bgcolor="white">
<span><H1>Server Error in '/' Application.<hr width=100% size=1 color=silver></H1>
<h2> <i>Request format is unrecognized for URL unexpectedly ending in '/HelloWorld'.</i> </h2></span>
<font face="Arial, Helvetica, Geneva, SunSans-Regular, sans-serif ">
<b> Description: </b>An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
<br><br>
<b> Exception Details: </b>System.InvalidOperationException: Request format is unrecognized for URL unexpectedly ending in '/HelloWorld'.<br><br>
<b>Source Error:</b> <br><br>
<table width=100% bgcolor="#ffffcc">
<tr>
<td>
<code>
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.</code>
</td>
</tr>
</table>
<br>
<b>Stack Trace:</b> <br><br>
<table width=100% bgcolor="#ffffcc">
<tr>
<td>
<code><pre>
[InvalidOperationException: Request format is unrecognized for URL unexpectedly ending in '/HelloWorld'.]
System.Web.Services.Protocols.WebServiceHandlerFactory.CoreGetHandler(Type type, HttpContext context, HttpRequest request, HttpResponse response) +405961
System.Web.Services.Protocols.WebServiceHandlerFactory.GetHandler(HttpContext context, String verb, String url, String filePath) +212
System.Web.Script.Services.ScriptHandlerFactory.GetHandler(HttpContext context, String requestType, String url, String pathTranslated) +47
System.Web.HttpApplication.MapHttpHandler(HttpContext context, String requestType, VirtualPath path, String pathTranslated, Boolean useAppConfig) +193
System.Web.MapHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +93
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155
</pre></code>
</td>
</tr>
</table>
<br>
<hr width=100% size=1 color=silver>
<b>Version Information:</b> Microsoft .NET Framework Version:2.0.50727.3082; ASP.NET Version:2.0.50727.3082
</font>
</body>
</html>
<!--
[InvalidOperationException]: Request format is unrecognized for URL unexpectedly ending in '/HelloWorld'.
at System.Web.Services.Protocols.WebServiceHandlerFactory.CoreGetHandler(Type type, HttpContext context, HttpRequest request, HttpResponse response)
at System.Web.Services.Protocols.WebServiceHandlerFactory.GetHandler(HttpContext context, String verb, String url, String filePath)
at System.Web.Script.Services.ScriptHandlerFactory.GetHandler(HttpContext context, String requestType, String url, String pathTranslated)
at System.Web.HttpApplication.MapHttpHandler(HttpContext context, String requestType, VirtualPath path, String pathTranslated, Boolean useAppConfig)
at System.Web.HttpApplication.MapHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)
-->
编辑:
所以我从url中删除了/ HelloWorld / part,并且响应没有生成 错误了。但是,这并没有解决我的问题。我在想另一个 传入方法名称的方式来调用?
答案 0 :(得分:0)
您的第一个示例使用数据类型json,但您的第二个示例使用自动完成使用xml。改为json?
答案 1 :(得分:0)
好的,所以我明白了。
这是JörnZaefferer的jquery autocomplete插件1.1
首先,您需要按以下方式修改jquery.autocomplete.js文件:
Find the function $.fn.extend({
under the function, find the line: url: isUrl ? urlOrData : null,
under that line, insert the following line: type: isUrl ? null : urlOrData,
Then find the function request(term, success, failure) {
under it, find the line: port: "autocomplete" + input.name,
under the line, add the following line: type: options.type,
这允许您指定请求的类型。如果我们想要调用web服务的web方法,我们想要指定“POST”作为选项。
现在,当您按原样执行自动完成调用时,请指定以下内容:
type: "POST",
dataType: "xml"
这告诉jquery ajax对方法发帖。瞧,它有效!