繁体   English   中英

WCF json POST请求方法:GET和405(不允许使用方法)

[英]WCF json POST Request Method:GET and 405 (Method Not Allowed)

你好,我的问题是我使用Microsoft Visual Studio创建WCF Web服务,当我在Microsoft Visual Studio上运行时,一切都很好并且可以工作。 但是我必须连接到外部的Web服务,所以当我这样做时它不会连接。 所以首先它给我crossDomain错误,所以我更改了webconfig,我从外部html文件中获取了值,但我无法发布。 我给了这个错误:

GET http://localhost:3281/UserService.svc/SetUser?callback=jQuery11020891759618…20%22usertype%22:%20%22%22,%20%22email%22%20:%20%22%22%20}&_=1386343306675 405 (Method Not Allowed)

我的英语不好,所以我将添加我的代码和源文件,您可以看到自己。

首先我的JavaScript:

    <script>

function btnSubmit() {
$.support.corps = true;



        $.ajax({
              crossDomain: true,
            cache: false,
            async: false,
            type: "POST",
            url: "http://localhost:3281/UserService.svc/SetUser",
            data: '{ "usertype": "' + $("#txtuserName").val() + '", "email" : "' + $("#txtuserEmail").val() + '" }',
            contentType: "application/json;charset=utf-8",
            dataType: "jsonp",

            success: function (r) { alert("Successfully Registered!!!"); },
            error: function (e) { alert(e.statusText); }
        });
    }

    function btnRetrieve() {
    $.support.corps = true;
        $.ajax({
              crossDomain: true,
            cache: false,
            async: false,
            type: "GET",  
            url: "http://localhost:3281/UserService.svc/GetUser",
            data: { name: $("#find").val() },
            contentType: "application/json;charset=utf-8",
            dataType: "jsonp",
            success: function (r) {
                if (r != null) $("#DisplayResult").html(r.Email);
                 else
                 $("#DisplayResult").html("Bilgi yok");
            },
            error: function (e) { alert(e.statusText); }
        });
    }



  </script>

我的服务内容:

namespace JQueryCallToWcf
{
    [ServiceContract(Namespace = "")]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class UserService
    {
        // Create a List of User type and add temporary data. This List will be used a 
        // Fake Repository for Data Tranasaction. In real world we can replace this with 
        // EntityFramework or Linq2Sql Data Model for actual data transactions.

        public static List<User> lstUsers = new List<User>()
            {
                new User() { Name="Rami", Email="Rami@Rami.com"},
                new User() { Name="Bill", Email="Bill@Bill.com"},
                new User() { Name="Mark", Email="Mark@Mark.com"},
            };

        // We have two service methods - SetUser and GetUser, which sets and gets 
        // user from fake repository.

        [OperationContract]
        [WebInvoke(
            Method = "POST",
            BodyStyle = WebMessageBodyStyle.WrappedRequest,
            RequestFormat = WebMessageFormat.Json,
            ResponseFormat = WebMessageFormat.Json)]
        public void SetUser(string usertype, string email)
        {
            lstUsers.Add(new User() { Name = usertype, Email = email });
        }

        [OperationContract]
        [WebGet(
            ResponseFormat = WebMessageFormat.Json)]
        public User GetUser(string name)
        {
            User op = lstUsers.Where(p => p.Name == name).FirstOrDefault();
            return op;
        }
    }

    // This is the User Class, holding properties for Name and email.

    [DataContract]
    public class User
    {
        [DataMember]
        public string Name { get; set; }

        [DataMember]
        public string Email { get; set; }
    }



}

我为跨域添加webconfig

 <system.serviceModel>
    <behaviors>
      <endpointBehaviors>
        <behavior name="webHttpBehavior">
          <webHttp />
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <bindings>
      <webHttpBinding>
        <binding name="webHttpBindingWithJsonp" crossDomainScriptAccessEnabled="true" />
      </webHttpBinding>
    </bindings>
    <services>
      <service name="JQueryCallToWcf.UserService">
        <endpoint address="" binding="webHttpBinding"
                  bindingConfiguration="webHttpBindingWithJsonp"
                  contract="JQueryCallToWcf.UserService"
                  behaviorConfiguration="webHttpBehavior"/>
      </service>
    </services>
  </system.serviceModel>

我给你我的解决方案文件的工作,我也给你以外的html文件获取工作后不。

http://s3.dosya.tc/server14/y4vqdP/Desktop.rar.html

我使用了jsonp,并更改了web.config中的绑定。 现在正在工作。

您可以这样尝试:

<system.webServer>
    <httpProtocol>
      <customHeaders>
        <add name="Access-Control-Allow-Origin" value="*" />
      </customHeaders>
    </httpProtocol>
        <handlers>
            <remove name="OPTIONSVerbHandler" />
            <add name="OPTIONSVerbHandler" path="*" verb="OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" resourceType="Unspecified" requireAccess="None" preCondition="bitness32" />
        </handlers>
  </system.webServer>

暂无
暂无

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

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