简体   繁体   English

WCF服务在JavaScript中返回“未定义”

[英]WCF service returning “is undefined” in JavaScript

When this JavaScript code is run, it tells me that "0x800a1391 - JavaScript runtime error: 'InputService' is undefined". 运行此JavaScript代码时,它告诉我“ 0x800a1391-JavaScript运行时错误:'InputService'未定义”。

I have tried and tried, and I just can't seem to figure out of what I am missing... 我已经尝试了很多次,但似乎无法弄清我所缺少的...

Web.Config file (just the web service part): Web.Config文件(仅是Web服务部分):

  <system.serviceModel>
    <behaviors>
      <endpointBehaviors>
        <behavior name="CommonEndPointBehavior">
          <enableWebScript/>
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>
    <services>
      <service name="InputService">
        <endpoint name="" address="" behaviorConfiguration="CommonEndPointBehavior" binding="webHttpBinding" contract="InputService" bindingConfiguration="webBinding" />
      </service>
    </services>
    <bindings>
      <webHttpBinding>
        <binding name="webBinding">
          <!--<security mode="Transport">-->
          <security mode="None"/>
        </binding>
      </webHttpBinding>
    </bindings>
  </system.serviceModel>

The Service: 服务:

[ServiceContract(Namespace = "")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class InputService
{
[OperationContract]
public string EditSiteElement(int siteid, string name, string url, string description, int siteorder, bool active)
{
    return Input.EditSiteElement(siteid, name, url, description, siteorder, active);
}
}

The references in the web form: Web表单中的参考:

    scriptManagerProxy.Services.Add(new ServiceReference("~/User/Input.svc"));
    scriptManagerProxy.Scripts.Add(new ScriptReference("~/User/Input.js"));

JavaScript file: JavaScript文件:

//When edit button is clicked on row.
function EditSiteElement(siteid) {
    InputService.GetSiteIdInfo(siteid, function (result) {
        var stuff = result.split('¤');
        $('[id$=TextBox_name]').val(stuff[0]);
        $('[id$=TextBox_link]').val(stuff[1]);
        $('[id$=TextBox_description]').val(stuff[2]);
        $('[id$=CheckBox_active]').prop('checked', (stuff[3] == 'True'));
        $('[id$=TextBox_order]').val(stuff[4]);
        //Open the dialog
        $("[id$=panel_Input]").dialog('open');

        SiteIdForSave = siteid;
    });
}

So, there are a couple of changes you have to do. 因此,您需要做一些更改。

First, decorate the service method with the WebInvoke attribute which resides in the System.ServiceModel.Web namespace (you may have to add the reference to your project). 首先,使用位于System.ServiceModel.Web命名空间中的WebInvoke属性装饰服务方法(您可能必须将引用添加到项目中)。

[OperationContract]
[System.ServiceModel.Web.WebInvoke] //add this attribute
public string EditSiteElement(int siteid, string name, string url, string description, int siteorder, bool active)
{
    return Input.EditSiteElement(siteid, name, url, description, siteorder, active);
}

Second, in the InputService.svc file (in Visual Studio, right click on the InputService.svc file and select View Markup ), add the Factory="System.ServiceModel.Activation.WebScriptServiceHostFactory" attribute: 其次,在InputService.svc文件中(在Visual Studio中,右键单击InputService.svc文件并选择View Markup ),添加Factory="System.ServiceModel.Activation.WebScriptServiceHostFactory"属性:

<%@ ServiceHost Language="C#" Debug="true" Factory="System.ServiceModel.Activation.WebScriptServiceHostFactory" Service="WebApplication6.InputService" CodeBehind="InputService.svc.cs" %>

Make sure that the target framework version for your application is 4.5 . 确保您的应用程序的目标框架版本为4.5

[EDIT] [编辑]

I suggest you modify the web.config 's <system.serviceModel> section as follows. 我建议您如下修改web.config<system.serviceModel>部分。 Please pay attention to the use of your namespaces ( MyNamespace ) and to the fact that I moved the behavior definition from the end point to the service level. 请注意使用命名空间( MyNamespace ),以及将行为定义从端点移到服务级别这一事实。

<system.serviceModel>
  <behaviors>
    <serviceBehaviors>
      <behavior name="InputServiceBehavior">
        <serviceMetadata httpGetEnabled="true"/>
        <serviceDebug includeExceptionDetailInFaults="false"/>
      </behavior>
    </serviceBehaviors>
  </behaviors>
  <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>
  <services>
    <service behaviorConfiguration="InputServiceBehavior" name="MyNamespace.InputService">
      <endpoint address="" binding="webHttpBinding" contract="MyNamespace.InputService" bindingConfiguration="webBinding"/>
    </service>
  </services>
  <bindings>
    <webHttpBinding>
      <binding name="webBinding">
        <!--<security mode="Transport">-->
        <security mode="None"/>
      </binding>
    </webHttpBinding>
  </bindings>
</system.serviceModel>

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

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