2011-07-06 4 views
6

を受け入れされていない私は、WCF 4 RESTサービスと通信するには、以下のjQueryの\ JavaScriptコードを使用しています。は、WCFと通信するためのjQueryのポストを取得しようとするが、JSONデータは

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

私はそれは私がユーザーデータを渡さない場合に、サービスが正しく偽の値を返すことである理由を確立しようとしています:

$.post(serviceUrl , loginSuccess); 

私はユーザーデータを渡す行うときとは対照的に、その時点でPOST 400(Bad Request)エラーが発生します。

$.post(serviceUrl , userInfo, loginSuccess); 

私はそれがJSONオブジェクト、のUserInfoは、内蔵されたり解釈されているどのように関係していると確信している、と私はバイオリン弾き2投稿できるか、それが役立つ場合WireSharkのは、ダンプします。ただ、私は本当にサービスのWCF側の変更へのアクセス権を持っていないので、うまくいけば、私はこの仕事をするために私の側で行うことができる何かがある...

を教えてください。

ありがとうございます!

編集私は少しより多くの情報を得ました

...どうやら、問題は、サーバーが次のエラーで応答していることである。

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)

は、だから私は、私が把握する必要があり考えていますオブジェクトをJQuery.post()メソッドを介してJSONオブジェクトとしてワイヤに渡す方法を説明します。

詳しい情報---再び...

OK ...何のapp.configファイルまたはweb.configファイルには、などがありません。

私はここまで、私は契約やコード、何を得ることができます。

[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

あなたのWCFサービスの設定を投稿してください。 –

+0

残念ながら、私はそれにアクセスできません... – Joe

答えて

7

私の質問に対する答えが見つかりました。

誰かこれにはJSON.stringify()メソッドの言及と正しい方向に私を指すようにしようとしましたが、それは私にそれが正しく実装得るために少しの努力をしました。

最後に、私はWireSharkを使用してhttpリクエストを盗聴し、実際に何が送受信されているかを確認し、データを盗聴する必要があるだけでなく、コンテンツタイプを指定する必要があることも観察しました。

ここに最終コードがあります。

// 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

サービスのインターフェイスでは、OperationalContract属性が必要で、その属性では、RequestFormatを設定する必要があります。以下は、どのように見えるかの例です。

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

私はあなたの提案を実装しようとしましたが、それは働いていないようです... '[OperationContract、WebInvoke(METHOD = "POST"、 RequestFormat = WebMessageFormat.Json、 ResponseFormat = WebMessageFormat.Json、 bodyStyle属性= WebMessageBodyStyle.WrappedRequest)] BOOLログイン(XiineUserInformationユーザ);' – Joe

関連する問題