2011-07-06 18 views
6

Tôi đang sử dụng mã JQuery \ JavaScript sau đây để giao tiếp với một dịch vụ REST WCF 4.Cố gắng để có được một bài đăng JQuery để giao tiếp với WCF, nhưng dữ liệu JSON không được chấp nhận

<script> 
var serviceUrl = "http://services.xiine.com/Xiine/Live/AccountService/rest/json/Login"; 
var userInfo = { 
    "IsNotEncrypted":true, 
    "Password":null, 
    "UserName":null 
}; 

var loginSuccess = function(data, textStatus, jqXHR){ 
console.log("yay"); 
}; 
var loginError = function(){ 
console.log("oh no"); 
}; 

$.post(serviceUrl , userInfo, loginSuccess); 
$.post(serviceUrl , loginSuccess); 

</script> 

Tôi cố gắng để thiết lập do tại sao nó là dịch vụ sẽ trở lại một cách chính xác một giá trị sai khi tôi không vượt qua dữ liệu người dùng:

$.post(serviceUrl , loginSuccess); 

Trái ngược với khi tôi vượt qua dữ liệu người dùng, tại thời điểm đó nó cho lỗi POST 400 (Yêu cầu không hợp lệ).

$.post(serviceUrl , userInfo, loginSuccess); 

Tôi chắc chắn nó phải làm với cách đối tượng JSON, userInfo, đang được xây dựng hoặc diễn giải, và tôi có thể đăng Fiddler 2 hoặc WireShark bãi, nếu điều đó sẽ giúp ích. Chỉ cần cho tôi biết ...

Tôi không thực sự có quyền truy cập để thay đổi bên WCF của dịch vụ, vì vậy hy vọng có điều gì đó mà tôi có thể làm để kết thúc công việc này.

Cảm ơn!

Sửa

tôi đã thêm một chút thông tin ... Rõ ràng, vấn đề là máy chủ đang phản hồi bằng các lỗi sau:

The server encountered an error processing the request. The exception message is 'The incoming message >has an unexpected message format 'Raw'. The expected message formats for the operation are 'Xml', >'Json'. This can be because a WebContentTypeMapper has not been configured on the binding. See the >documentation of WebContentTypeMapper for more details.'. See server logs for more details. The >exception stack trace is:

at System.ServiceModel.Dispatcher.DemultiplexingDispatchMessageFormatter.DeserializeRequest(Message message, Object[] parameters) at System.ServiceModel.Dispatcher.UriTemplateDispatchFormatter.DeserializeRequest(Message message, Object[] parameters) at System.ServiceModel.Dispatcher.CompositeDispatchFormatter.DeserializeRequest(Message message, Object[] parameters) at System.ServiceModel.Dispatcher.DispatchOperationRuntime.DeserializeInputs(MessageRpc& rpc) at System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage41(MessageRpc& rpc) at System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet)

Vì vậy, tôi nghĩ tôi cần phải hình làm thế nào để đưa đối tượng đi qua dây như một đối tượng JSON thông qua một phương thức JQuery.post().

biết thêm thông tin --- Một lần nữa ...

ok ... Không có app.config hoặc web.config, như vậy.

Đây là những gì tôi có thể nhận được như xa như các hợp đồng và mã và những gì không.

[ServiceContract] 
public interface IAccount 
{ 
[OperationContract] 
bool Login(UserInformation user); 
} 

[ServiceBehavior(IncludeExceptionDetailInFaults = true)] 
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] 
public class AccountService : IAccount 
{ 
public bool Login(UserInformation user) 
{ 
    //stuff... 
} 
} 

[DataContract] 
public class UserInformation 
{ 
[DataMember(IsRequired = true)] 
public string UserName; 

[DataMember(IsRequired = true)] 
public string Password; 

[DataMember(IsRequired = true)] 
public bool IsNotEncrypted; 
} 

public interface IUserInformation 
{ 
UserInformation UserInformation { get; set; } 
} 
+0

Vui lòng đăng cấu hình dịch vụ WCF của bạn. –

+0

Thật không may, tôi không có quyền truy cập vào nó ... – Joe

Trả lời

7

Vì vậy, tôi đã tìm thấy câu trả lời cho câu hỏi của mình.

Ai đó đã cố gắng chỉ cho tôi đúng hướng với đề cập đến phương thức JSON.stringify(), nhưng tôi đã nỗ lực một chút để làm cho nó được triển khai đúng cách. Cuối cùng, tôi đã sử dụng WireShark để tìm ra yêu cầu http, xem những gì đã thực sự được gửi và nhận, và quan sát rằng không chỉ tôi cần phải xác định dữ liệu, nhưng tôi cũng cần xác định loại nội dung.

Đây là mã cuối cùng.

// This is the URL that is used for the service. 
var serviceUrl = "http://services.xiine.com/Xiine/Live/AccountService/rest/json/Login"; 


// This is the JSON object passed to the service 
var userInfo = { 
    "UserName": null, 
    "Password": null, 
    "IsNotEncrypted": true 
}; 

// $.ajax is the longhand ajax call in JQuery 
$.ajax({ 
    type: "POST", 
    url: serviceUrl, 
    contentType: "application/json; charset=utf-8", 

    // Stringify the userInfo to send a string representation of the JSON Object 
    data: JSON.stringify(userInfo), 
    dataType: "json", 
    success: function(msg) { 
     console.log(msg); 
     }}); 
0

trong giao diện cho dịch vụ của bạn, bạn cần có thuộc tính OperationalContract và trong thuộc tính đó, bạn cần đặt RequestFormat. Đây là một ví dụ về những gì nó có thể trông như thế nào.

[OperationContract, WebInvoke(UriTemplate = "/runGenericMethodJSON", Method = "POST", 
     RequestFormat = WebMessageFormat.Json, 
     ResponseFormat = WebMessageFormat.Json, 
     BodyStyle = WebMessageBodyStyle.WrappedRequest)] 
+0

Tôi đã cố gắng thực hiện đề xuất của bạn, nhưng có vẻ như không hoạt động ... '[OperationContract, WebInvoke (Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, bodystyle = WebMessageBodyStyle.WrappedRequest)] bool Đăng nhập (user XiineUserInformation);' – Joe

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