为什么405方法不允许在wcf中出错?

时间:2015-01-08 07:55:41

标签: c# jquery ajax wcf

这是我的班级

[DataContract()]
    public class Test
    {
        [DataMember]
        public string UserName { get; set; }

    }

这是我的svc.cs页面功能

public string Testpost(Test T)
        {
            string User_name;
            User_name = T.UserName;

            DataUtility du = new DataUtility();

            string[] Parameters = new string[2] { "@UserName", "@Result" };
            string[] DbTypes = new string[2] { "varchar", "varchar" };
            string[] ParameterTypes = new string[2] { "input", "Output" };
            string[] values = new string[2] { User_name, "" };
            string[] Lengths = new string[2] { "100", "50" };


            String Praveen = du.executeNonQuerry(CommandType.StoredProcedure, "InsertTest", Parameters, DbTypes, ParameterTypes, values, Lengths);

            if (Praveen == "1")
            {
                return "Insertion Success";
            }
            else
            {
                return "Failed Insertion";
            }


        }

这是我的uri模板

 [OperationContract]
        [WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare, UriTemplate = "/Testpost")]
        string Testpost(Test T);

这是我的配置

<service name="SkyService.Customer" behaviorConfiguration="Default">
        <endpoint address="" binding="webHttpBinding" contract="SkyService.ICustomer" behaviorConfiguration="webBehavior" bindingConfiguration="WCFServiceBinding"/>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
      </service>

我通过j查询ajax访问post方法,如as

<script type="text/javascript">
         $(function () { 
             //var param = {"UserName":"Praveen"};

            var _Name = { "T": { "UserName": "LC" } };
            $.support.cors = true;
             $.ajax({
                 type: "POST",
                 url: "http://203.124.96.81/Track/Customer.svc/TestPost",

                 //data: {"UserName":"Praveen"},
                 //contentType: "application/json",
                 //dataType: 'jsonp',

                 data: JSON.stringify(_Name),
              returncallback: false,
                 contentType: "application/json",
                 dataType: "jsonp",


                 success: function (data) {
                     alert(data.Text);
                     console.log(data);

                 },
                 error: function (XMLHttpRequest, textStatus, errorThrown) {
                     //alert(XMLHttpRequest+" "+textStatus+" "+errorThrown);
                     console.log(XMLHttpRequest);
                     console.log(textStatus);
                     console.log(errorThrown);

                 }
             });
         })

    </script>

服务获取结果在localhost中并且数据库中的数据插入成功,但是当我通过远程使用它时出现错误 METHOD NOT NOTOWED ????怎么做???

1 个答案:

答案 0 :(得分:0)

我认为这是一个跨域问题。

请按照http://www.dotnet-tricks.com/Tutorial/wcf/X8QN260412-Calling-Cross-Domain-WCF-Service-using-Jquery.html

中的说明操作

并尝试一下。