我有一个 Web 应用程序。在这个应用程序中,我添加了一个启用 AJAX 的 WCF 服务。名称是 PricingService,代码如下:
namespace PricingService
{
[ServiceContract(Namespace = "PricingService")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class PricingService
{
[OperationContract]
[WebInvoke]
public double CalculatePrice(string itemId, string shipToPostalCode)
{
double price;
price = 45;
price = price * 1.06;
return price;
}
}
}
我想从 ASP.net 中的网站从 AJAX 调用此服务。在 default.aspx 页面中,我添加了以下代码:
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server">
<Services>
<asp:ServiceReference Path="http://localhost:49340/PricingService.svc" />
</Services>
</asp:ScriptManager>
Product: <br />
<asp:TextBox ID="txtProduct" runat="server"></asp:TextBox>
<br />
Ship to (Postal code ):<br />
<asp:TextBox ID="txtPostCode" runat="server"></asp:TextBox>
<br />
<br />
<input id="ButtonCalculate" type="button" value="Get Price" onclick="buttonCalculate_onClick()"/>
<br />
<br />
<asp:Label ID="LabelPrice" runat="server" Text="Label"></asp:Label>
</div>
<script language="javascript" type="text/javascript">
function buttonCalculate_onClick() {
var service = new PricingService.PricingService();
service.CalculatePrice(document.form[0].txtProduct.value,document.form[0].txtPostCode.value,onSucces,onFail,null);
}
function onSuccess(result) {
LabelPrice.innerText = result;
}
function onFail(result) {
alert(result);
}
</script>
</form>
</body>
当我单击按钮时,我收到消息:'PricingService' 未定义 有人可以告诉我有什么问题吗?网站和 wcf 服务在同一个名为“WcfServiceApplication”的项目中 谢谢!