调用WebMethod时找不到方法

本文关键字:方法 找不到 WebMethod 调用 | 更新日期: 2023-09-27 18:02:35

服务:

   public string Create(RestRegist objname)
   {
       RestRegist objrest = new RestRegist();
       return objrest.Create(objname);
   }

I服务

【运营合同】

    [WebInvoke(Method = "POST",
        RequestFormat = WebMessageFormat.Json,
        ResponseFormat = WebMessageFormat.Json,
        BodyStyle = WebMessageBodyStyle.Wrapped,
        UriTemplate = "/AddData")]
        string AddCreate(RestRegist objname);

Web配置:

<services>
  <service name="SampleRestReg.Service1" behaviorConfiguration="ServiceBehaviour">
    <endpoint address="" binding="webHttpBinding" contract="SampleRestReg.IService1" behaviorConfiguration="ServiceBehaviour" ></endpoint>
    <host>
      <baseAddresses>
        <add baseAddress="http://mylocalhost/Service1.svc"/>
      </baseAddresses>
    </host>
  </service>
</services>
<behaviors>
  <serviceBehaviors>
    <behavior name="ServiceBehaviour">
      <serviceMetadata httpGetEnabled="true" />
      <serviceDebug includeExceptionDetailInFaults="true" />
    </behavior>
  </serviceBehaviors>
  <endpointBehaviors>
    <behavior name="ServiceBehaviour">
      <webHttp/>
    </behavior>
  </endpointBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />

我的html页面

Ajax代码:

   $("#btnsave").click(function () {
       var name = $("#patient").val();
       var age = $("#Ag").val();
       var gender = $("#gender").val();
       var address = $("#ad1").val();
       var city = $('#city').val();
       var mobile = $('#phn').val();
       var depart = $('#Select2').val();
       var doctor = $('#Select3').val();
       $.ajax({
           Type: "POST",
           processData: true,
           dataType: "json",
           url: "http://localhost:51341/Service1.svc/AddData",
           data: '{ PatientName: "name", Age: "age", Gender:"gender", Address1:"address", City:"city", Phone:"mobile", Department: "depart", Doctor:"doctor" }',
           contentType: "application/json; charset=utf-8",
           success: function (data, status, jqXHR) {
               alert('Record Save Successfully')
           },
           error: function (xhr) {
               alert(xhr.responseText);
           }
       });
   });
        </script>

这是我的html页面和ajax代码,

我找不到获取方法,当我点击保存按钮时,我正在使用Rest wcf服务

我什么都试过了,但没有用。。plz帮我解决

调用WebMethod时找不到方法

我想这行需要一些参数。

在javascript中,根据HTML中的控件填充这些参数。

var name = ...;
...
data: '{ PatientName: ''' + name
      + ''', Age: ' + age
      + ', Gender: ' + gender
      + ', Address1: '''
      + address + ''', City: '''
      + city + ''', Phone: '''
      + mobile + ''', Department: '''
      + depart + ''', Doctor: ''' + doctor
      + ''' }'

我在REST实践中对WCF服务有一些经验。你能访问http://yourlocalhost/Service1.svc/jsdebug吗?如果你不能访问这个代理,你会遇到javascript代理生成的第一个问题。。。如果是,请参见以下内容:

在web.config,behavior.endpointbehavior部分,您可以尝试添加这个标签吗

<behaviors>
    <endpointBehaviors>
        <behavior name="YourBehaviorName">
            <webHttp />
            <enableWebScript />
        </behavior>
    <endpointBehaviors>
<behaviors>

祝你好运!从这些东西开始并不容易。