简体   繁体   English

如何从javascript调用WCF服务?

[英]How call WCF service from javascript?

I have a WCF service in folder "Services": 我在“服务”文件夹中有一个WCF服务:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Web.Services;
using System.ServiceModel.Activation;
using System.Web.Script.Services;

namespace Application.Services
{
    [ServiceContract]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    [ScriptService]    
    public class ProductTypeService
    {
        [OperationContract]
        [WebMethod]
        public string LoadMainGridProductType(string test)
        {
            return "Success";
        }
    }
}

and on page I try to call this service method from javascript: 在页面上,我尝试从javascript调用此服务方法:

$(document).ready(function () {
    var counter = "test";    
    Application.Services.ProductTypeService.LoadMainGridProductType(counter, ResultLoadMainGridProductType, ErrorLoadMainGridProductType);
});

on page I also make conect to this service and javascript files: 在页面上,我还对此服务和javascript文件进行了补充:

<asp:ScriptManager ID="ScriptManager" runat="server">
            <Scripts>
                <asp:ScriptReference Path="~/Scripts/ProductType.js" />
            </Scripts>
            <Services>
                <asp:ServiceReference Path="~/Services/ProductTypeService.svc" />        
            </Services>
        </asp:ScriptManager>

but code fails on 但是代码失败

Application.Services.ProductTypeService.LoadMainGridProductType(counter, ResultLoadMainGridProductType, ErrorLoadMainGridProductType);

Error : Error of implementation of Microsoft Jscript: "Application" is not certain. 错误:Microsoft Jscript的实现错误:不确定“应用程序”。 How fix this??? 如何解决这个问题???

Firstly, check to see if the WCF service is working correctly by navigating directly to it, eg http://localhost:(port-number)/Services/ProductTypeService.svc 首先,通过直接导航到WCF服务来检查它是否正常运行,例如http:// localhost:(port-number)/Services/ProductTypeService.svc

I suspect that this wont work and give you some errors. 我怀疑这将无法正常工作,并给您一些错误。

Secondly, check to make sure that the jsdebug javascript file has been generated and is included in the scripts loaded with the application. 其次,检查以确保jsdebug javascript文件已生成并包含在与应用程序一起加载的脚本中。

Another person asked a similar question earlier. 另一个人早些时候问过类似的问题。 How get namespace on javascript in asp.net web? 如何在ASP.NET Web中的javascript上获取名称空间?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM