调用WebMethod时找不到的方法

时间:2014-05-09 08:06:39

标签: c# asp.net-mvc-4

服务:

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

   }

IService

[OperationContract的]

    [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帮助我

2 个答案:

答案 0 :(得分:0)

我想这行需要一些参数。

在您的javascript中根据HTML中的控件填写这些参数。

var name = ...;
...

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

答案 1 :(得分:0)

我在REST练习中有一些WCF服务的经验。 你可以访问http://yourlocalhost/Service1.svc/jsdebug吗?如果您无法访问此代理,则首先出现javascript代理生成问题...如果是这样,请参阅以下内容:

在web.config中,behavior.endpointbehavior部分可以尝试添加此标记

<behaviors>
    <endpointBehaviors>
        <behavior name="YourBehaviorName">
            <webHttp />
            <enableWebScript />
        </behavior>
    <endpointBehaviors>
<behaviors>
祝你好运!从这些东西开始并不容易。