Trying to get a jQuery message to communicate with WCF, but JSON data is not accepted

I use the following jQuery \ JavaScript code to communicate with WCF 4 REST service.

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

I am trying to establish why the service will correctly return a false value when I do not transfer user data:

 $.post(serviceUrl , loginSuccess); 

Unlike when I pass user data, at that moment it causes a POST 400 error (failed request).

 $.post(serviceUrl , userInfo, loginSuccess); 

I am sure this is due to the way the JSON object, userInfo, is constructed or interpreted, and I can post Fiddler 2 or WireShark dumps if that helps. Just let me know ...

I do not have access to change the WCF service, so I hope there is something I can do on my part to make this work.

Thanks!

Edit

I got a little more information ... Apparently, the problem is that the server is responding with the following error:

The server encountered an error processing the request. Exception message: Incoming message has an unexpected Raw message format. The expected message formats for the operation are "Xml", "Json". This may be due to the fact that WebContentTypeMapper was not configured to bind. See the documentation> WebContentTypeMapper for more details. '. See Server Logs for more information. Exception stack trace:

in System.ServiceModel.Dispatcher.DemultiplexingDispatchMessageFormatter.DeserializeRequest (Message message, Object [] parameters) in System.ServiceModel.Dispatcher.UriTemplateDispatchFormatter.DeserializeRequest (Message message, Object [] parameters) in System.ServiceModel.DispatcherFormatter. Parameters DeserializeRequest (Message message, Object []) in System.ServiceModel.Dispatcher.DispatchOperationRuntime.DeserializeInputs (MessageRpc & rpc) in System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin (MessageRpc & rpc) in System.ServiceMatchPermodeMatchPerviceMatchPerviceMatchPerviceMatchPerviceMatchPerviceMatchPerviceMatchPerviceMatchPerMentPerMentPerMentPerPerviceMatchPerviceMePermPerMentPerMentPervicePervice (MessageRpc & rpc) in System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage41 (MessageRpc & rpc) in System.ServiceModel.Dispatcher.MessageRpc.Process (Boolean isOperationContextSet)

So, I think I need to figure out how to make the object go through the wire as a JSON object using the JQuery.post () method.

Further information --- Again ...

ok ... No app.config or web.config as such.

Here's what I can get before contracts and code, and what not.

 [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; } } 
+6
source share
2 answers

So, I found the answer to my question.

Someone tried to point me in the right direction with reference to the JSON.stringify () method, but it took me a bit of effort to implement it correctly.

In the end, I used WireShark to sniff out an HTTP request, see what was actually sent and received, and watch that I not only needed to parse the data, but I also needed to specify the type of content.

Here is the final code.

 // 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); }}); 
+7
source

in the interface for your service, you need to have the OperationalContract attribute, and in this attribute you need to set RequestFormat. Here is an example of how it might look.

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

Source: https://habr.com/ru/post/892164/


All Articles