使用Javascript调用WCF服务时出现问题 [英] Problem in calling WCF Service using Javascript
问题描述
我从JavaScript调用WCF服务时遇到问题.
我正在使用ajax自动完成扩展程序(一种存储过程)从sql表中获取一堆记录.
我无法弄清楚哪里出了错.
谁能帮我吗?
Hi,
I have a problem in Calling the WCF Service from JavaScript.
I am using ajax autocomplete extender, a stored procedure, to fetch a bunch of records from sql table.
I am unable to figure out where I made a mistake.
Can anyone help me?
designer.cs
public System.Data.Linq.Table<icdcode> ICDCODEs
{
get
{
return this.GetTable<icdcode>();
}
}
}
[global::System.Data.Linq.Mapping.TableAttribute(Name="dbo.ICDCODE")]
public partial class ICDCODE
{
private string _CODE;
private string _DESCRIPTION;
public ICDCODE()
{
}
[global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_CODE", DbType="NVarChar(20) NOT NULL", CanBeNull=false)]
public string CODE
{
get
{
return this._CODE;
}
set
{
if ((this._CODE != value))
{
this._CODE = value;
}
}
}
[global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_DESCRIPTION", DbType="NVarChar(200) NOT NULL", CanBeNull=false)]
public string DESCRIPTION
{
get
{
return this._DESCRIPTION;
}
set
{
if ((this._DESCRIPTION != value))
{
this._DESCRIPTION = value;
}
}
GetDataCode.cs //service name.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Windows.Forms;
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "GetDataCode" in code, svc and config file together.
[ServiceContract(Namespace = "QAWorks")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class GetDataCode
{
public void DoWork()
{
}
public String[] getResult(string input)
{
string description=input;
String[] result;
using(DATADataContext db=new DATADataContext())
{
result = (from p in db.ICDCODEs
where p.DESCRIPTION.Contains(input)
select p.DESCRIPTION).ToArray();
}
return result;
}
}
<asp:TextBox ID="txtDiagnosis" runat="server" Width="150px" />;
<ajaxToolkit:TextBoxWatermarkExtender ID="AJXPDIAGNOSIS" runat="server" TargetControlID="txtDiagnosis" WatermarkCssClass="watermarkedSingleLine" WatermarkText="DIAGNOSIS" />;
<ajaxToolkit:AutoCompleteExtender ID="AJXFILLDIAGNOSIS" runat="server" CompletionInterval="100" DelimiterCharacters=" " Enabled="True" FirstRowSelected="True" MinimumPrefixLength="1" önClientItemSelected="FillDiagnosis(this)" ServiceMethod="getResult" ServicePath="~/Services/GetDataCode.svc" TargetControlID="txtDiagnosis">
Java script
function FillDiagnosis() {
var input = document.getElementById('<%=txtDiagnosis.ClientID%>').value;
GetDataCode.getResult(input, OnWSRequestComplete);
}
function OnWSRequestComplete(results) {
alert(results);
}
</icdcode></icdcode>
推荐答案
尝试更改以下内容:
Try changing the following:
var input = document.getElementById('<%=txtDiagnosis.ClientID%>').value;
至:
to:
var input = document.getElementById('txtDiagnosis').value; // edited from .Text to .value
要看的东西:
1. getResult
函数缺少[OperationContract]
.
2.根据文档,getResult
方法签名与AutoCompleteExtender
的必要签名不匹配. [ ^ ]用作ServiceMethod
.
3. GetDataCode.getResult传递两个参数,但是方法签名仅显示一个.
[/EDIT]
Things to look at:
1. [OperationContract]
is missing from getResult
function.
2. getResult
method signature does not match the necessary one for the AutoCompleteExtender
according to the documentation[^] for use as a ServiceMethod
.
3. GetDataCode.getResult is passing two parameters, however, the method signature only shows one.
[/EDIT]
Hurrah !!
我终于完成了任务.感谢大家的帮助. ;)
Hurrah!!
I completed my task finally. Thanks for the help to everyone. ;)
这篇关于使用Javascript调用WCF服务时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!