1

运行此 JavaScript 代码时,它告诉我“0x800a1391 - JavaScript 运行时错误:'InputService' 未定义”。

我已经尝试过,但我似乎无法弄清楚我错过了什么......

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>

服务:

[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);
}
}

网络表格中的参考资料:

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

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;
    });
}
4

1 回答 1

0

因此,您必须进行一些更改。

WebInvoke首先,使用位于命名空间中的属性装饰服务方法System.ServiceModel.Web(您可能必须添加对项目的引用)。

[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);
}

其次,在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" %>

确保您的应用程序的目标框架版本是4.5

[编辑]

我建议你修改web.config's<system.serviceModel>部分如下。请注意您的命名空间 ( 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>
于 2012-12-16T17:28:36.710 回答