WCF WebInvoke POST - 方法不允许错误

joc*_*cey 6 c# asp.net ajax wcf post

我有一个OperationContract方法,我试图查询并将数据插入数据库.我正在使用一种POST方法并在浏览器中从javascript调用服务.WCF服务位于同一个域中,因此我不必使用JSONP.我也在修改数据,因此它应该是POST请求而不是GET请求.但是我仍然得到"方法不允许错误".有谁遇到过这种情况?

我的服务正在调用

http://some_url.com/services/ProfileService.svc/json/CurrentUser
Run Code Online (Sandbox Code Playgroud)

奇怪的是,GET即使我指定了一个,当我去这个网址的时候似乎是通过请求调用的POST.但是,在页面加载时,它似乎正在尝试POST请求.

进入网址时浏览器响应:

Request URL:http://some_url.com/services/ProfileService.svc/json/CurrentUser
Request Method:GET
Status Code:405 Method Not Allowed
Request Headersview parsed
GET /services/ProfileService.svc/json/CurrentUser HTTP/1.1
Run Code Online (Sandbox Code Playgroud)

这是我试图打电话的方法:

[OperationContract]
[WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json)]

public HIPUser GetCurrentUser()
{
    string[] domainUser;
    string Auth_User = HttpContext.Current.User.Identity.Name.ToString().ToLower();
    domainUser = Auth_User.Split('\\');
    string user = domainUser[1];
    Debug.WriteLine(user);

    ProfileManager pm = new ProfileManager();
    var results = pm.GetUserByUserName(user);

    if (results.Length > 0)
    {
        return results.First();
    }
    else
    {
        Debug.WriteLine("IS NULL");
        var x = pm.CreateUser(user, null, null);
        Debug.WriteLine(x.UserName);
        return x;
    }
}
Run Code Online (Sandbox Code Playgroud)

客户:

function getCurrentUser() {

$.ajax({
    type: "POST",
    url: "services/ProfileService.svc/json/GetCurrentUser",
    contentType: "application/json; charset=utf-8",
    data: null,
    dataType: "json",
    error: function (request, error, u) {
        alert('blargherror: ' + error);
    },
    success: function (result, status) {
        alert(result.d);
    }
});
}
Run Code Online (Sandbox Code Playgroud)

不确定是否需要但是Web.Config:

<behaviors>
   <endpointBehaviors>
        <behavior name="jsonBehavior">
            <enableWebScript />
        </behavior>
    </endpointBehaviors>

    <serviceBehaviors>
        <behavior name="metaBehavior">
            <serviceDebug includeExceptionDetailInFaults="true" />
            <serviceMetadata httpGetEnabled="true" />
        </behavior>
    </serviceBehaviors>
</behaviors>

<serviceHostingEnvironment 
    aspNetCompatibilityEnabled="true"
    multipleSiteBindingsEnabled="true" />

<services>
    <service name="ProfileService" behaviorConfiguration="metaBehavior">
        <endpoint address="/json" 
            behaviorConfiguration="jsonBehavior"
            binding="webHttpBinding" 
            bindingConfiguration="secure" 
            contract="ProfileService" />
        <endpoint address="" 
            binding="basicHttpBinding" 
            bindingConfiguration="secure" 
            contract="ProfileService" />
    </service>
</services>
Run Code Online (Sandbox Code Playgroud)

编辑到Web.Config - 添加绑定

<bindings>
        <webHttpBinding>
            <binding name="secure">
                <security mode="TransportCredentialOnly">
                    <transport clientCredentialType="Windows"/>
                </security>
            </binding>
        </webHttpBinding>
        <basicHttpBinding>
            <binding name="secure">
                <security mode="TransportCredentialOnly">
                    <transport clientCredentialType="Windows" />
                </security>
            </binding>
        </basicHttpBinding>
    </bindings>
Run Code Online (Sandbox Code Playgroud)

joc*_*cey 1

我能够弄清楚这一点。返回对象是未知类型存在问题,因此无法将其序列化为对象JSON。不确定如何序列化对象以使其与我的方法一起使用,因此我最终只是更改方法以返回构造我自己的自定义字符串的字符串JSON,并使用该eval()函数将其转换为JSON对象。