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

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

本文关键字:方法 不允许 json POST 请求 WCF GET      更新时间:2024-04-17

你好,我的问题是我使用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文件get是工作岗位不是。

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>