2012-07-10 35 views
6

Tôi có phương thức OperationContract nơi tôi đang cố truy vấn và chèn dữ liệu vào cơ sở dữ liệu. Tôi đang sử dụng phương thức POST và gọi dịch vụ từ javascript trong trình duyệt. Dịch vụ WCF nằm trong cùng một miền, vì vậy tôi không cần phải sử dụng JSONP. Tôi cũng đang sửa đổi dữ liệu do đó, yêu cầu này phải là yêu cầu POST không phải là yêu cầu GET. Tuy nhiên tôi vẫn nhận được một "Phương pháp không được phép lỗi". Có ai gặp phải tình huống này không?WCF WebInvoke POST - Lỗi phương pháp không được phép

dịch vụ của tôi đang được gọi là tại

http://some_url.com/services/ProfileService.svc/json/CurrentUser 

Kỳ lạ thay, nó dường như được gọi thông qua một yêu cầu GET khi tôi đi url này mặc dù tôi thể chỉ định một POST. Tuy nhiên, trên tải trang, dường như đang thử yêu cầu POST.

phản ứng của trình duyệt khi đi url:

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 

Đây là phương pháp của tôi mà tôi đang cố gắng để gọi:

[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; 
    } 
} 

Chủ đầu tư:

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); 
    } 
}); 
} 

Không chắc nếu cần thiết nhưng 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> 

Edit để Web.Config - Thêm bindings

<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> 
+2

Điều này sẽ không giải quyết được vấn đề của bạn nhưng thực hành tốt để chuyển '" {} "' thành dữ liệu thay vì null (http://encosia.com/3-mistakes-to-avoid-when-using-jquery -with-aspnet-ajax /) – Jupaol

+0

Cảm ơn bài viết! – jocey

+2

Tôi không thể tìm thấy bất kỳ điều gì sai với mã của bạn, tôi vừa tạo một dịch vụ tương tự với cùng cấu hình và hoạt động chính xác, khác biệt duy nhất là 'bindingConfiguration =" secure "', tôi đoán bạn không đăng liên kết đó vì lý do bảo mật, tốt nếu bạn có thể đăng nó sẽ hữu ích. Bạn đã thử xóa cấu hình ràng buộc đó chưa và xem liệu bạn có thể truy cập dịch vụ của mình không ?? – Jupaol

Trả lời

1

tôi đã có thể con số này ra. Đã xảy ra sự cố với đối tượng trả về là loại không xác định, vì vậy nó không thể được tuần tự hóa thành đối tượng JSON. Không chắc chắn làm thế nào để tuần tự hóa đối tượng để làm việc với phương thức của tôi, vì vậy tôi đã thay đổi phương thức trả về một chuỗi xây dựng chuỗi JSON tùy chỉnh của riêng tôi và sử dụng hàm eval() để biến nó thành đối tượng JSON.

Các vấn đề liên quan