从 AutoCompleteExtender 调用启用 Ajax 的 WCF 应用程序
我正在尝试使用 WCF 服务来完成 MS Ajax AutoCompleteExtender 完成列表。我尝试了两种选择。如果我在网站项目中添加 WCF 服务,AutoCompleteExtender 会通过 POST 调用它并且工作正常。
然后我决定创建一个单独的 WCF 应用程序,并将支持 AJAX 的 WCF 服务添加到新应用程序中。我还复制了我网站的 Web.config
中有关 servicemodel
的部分内容。但这不起作用!首先,自动完成功能使用 GET
调用服务,而不是 POST
。我更改了服务的 WebInvokeAttribute
和 WebGet
以接受 GET
。现在,服务向扩展程序发送了正确的响应(我使用 Fiddler 观看了这一点),但扩展程序未填充完成列表。
扩展器定义如下(act
是 AjaxControlToolkit 的标记):
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server" autocomplete = "off"></asp:TextBox>
<act:AutoCompleteExtender ID="TextBox1_AutoCompleteExtender" runat="server"
DelimiterCharacters="" Enabled="True" ServiceMethod="GetNames"
ServicePath="http://localhost:4227/Service1.svc" TargetControlID="TextBox1">
</act:AutoCompleteExtender>
<asp:Button ID="Button1"
runat="server" Text="Button" />
</div>
<act:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
</act:ToolkitScriptManager>
</form>
WCF 服务工作在端口 4227 上。它由 Visual Studio 运行。在第一种情况下,ServicePath 是 Service1.svc
。
Web.Config 以这样的方式定义 sevicemodel:
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
<services>
<service name="WcfService1.Service1" behaviorConfiguration="WcfService1.Service1Behavior">
<endpoint address="" behaviorConfiguration="WcfService1.Service1AspNetAjaxBehavior" binding="webHttpBinding" contract="WcfService1.Service1" />
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="WcfService1.Service1AspNetAjaxBehavior">
<enableWebScript/>
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="WcfService1.Service1Behavior">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
那么,我有两个问题:
- 为什么在这种情况下 Autocomplete 使用不同的动词来发送请求?
- 为什么在第二种情况下不起作用?
我上传了示例解决方案来重现问题。
I'm trying to use a WCF service to fulfill MS Ajax AutoCompleteExtender completion list. I tried two alternatives. If I add a WCF service in my website project, AutoCompleteExtender calls it thriugh POST and it works fine.
Then I decided to make a separate WCF Application and add my AJAX-enabled WCF service to new application. I also copied part of Web.config
of my site concerning servicemodel
. And it doesn't work! First of all, autocomplete calls a service uing GET
, not POST
. I changed WebInvokeAttribute
and WebGet
of my service to accept GET
. Now the service sends a correct response to extender (I watched this using Fiddler) but extender doesn't fill completion list.
The extender is defined as follows (act
is a tag for AjaxControlToolkit):
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server" autocomplete = "off"></asp:TextBox>
<act:AutoCompleteExtender ID="TextBox1_AutoCompleteExtender" runat="server"
DelimiterCharacters="" Enabled="True" ServiceMethod="GetNames"
ServicePath="http://localhost:4227/Service1.svc" TargetControlID="TextBox1">
</act:AutoCompleteExtender>
<asp:Button ID="Button1"
runat="server" Text="Button" />
</div>
<act:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
</act:ToolkitScriptManager>
</form>
WCF service works on port 4227. It is running by Visual Studio. In the first case ServicePath is Service1.svc
.
Web.Config defines sevicemodel in a such way:
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
<services>
<service name="WcfService1.Service1" behaviorConfiguration="WcfService1.Service1Behavior">
<endpoint address="" behaviorConfiguration="WcfService1.Service1AspNetAjaxBehavior" binding="webHttpBinding" contract="WcfService1.Service1" />
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="WcfService1.Service1AspNetAjaxBehavior">
<enableWebScript/>
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="WcfService1.Service1Behavior">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
So, I have two auestions:
- Why in this cases Autocomplete uses different verbs to send a request?
- Why it doesn't work in the second case?
I uploaded a sample solution to reproduce problem.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
AutoCompleteExtender 使用 AJAX 来获取数据。不允许跨域 AJAX 请求。您的 Web 服务托管在
localhost:4227
上,您的 Web 应用程序托管在localhost:XXXX
上,其中XXXX
与 4227 不同。更多信息同源政策。
AutoCompleteExtender uses AJAX to fetch data. Cross domain AJAX requests are not allowed. Your web service is hosted on
localhost:4227
and your web application is hosted onlocalhost:XXXX
whereXXXX
is different than 4227.More info on Same origin policy.