Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to get a JQuery Post to communicate with WCF, but the JSON data is not being accepted

I am using the following JQuery\JavaScript code to communicate with a 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 it is that the service will correctly return a false value when I do not pass the user data:

$.post(serviceUrl , loginSuccess);

As opposed to when I do pass user data, at which point it gives a POST 400 (Bad Request) error.

$.post(serviceUrl , userInfo, loginSuccess);

I am sure it has to do with how the JSON object, userInfo, is being built or interpreted, and I can post Fiddler 2 or WireShark dumps, if that will help. Just let me know...

I don't really have access to changing the WCF side of the service, so hopefully there is something I can do on my end 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. 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)

So I am thinking I need to figure out how to get the object to go across the wire as a JSON object via a JQuery.post() method.

More information --- Again...

ok... There is no app.config or web.config, as such.

Here is what I can get as far as the 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; }
}
like image 698
Joe Avatar asked Jul 06 '11 18:07

Joe


1 Answers

So, I found the answer to my question.

Someone attempted to point me in the right direction with a mention of the JSON.stringify() method, but it took me a little effort to get it implemented correctly.

In the end, I used WireShark to sniff out the http request, see what was actually being sent and received, and observe that not only did I need to stingify the data, but I also needed to specify the content type.

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);
        }});
like image 194
Joe Avatar answered Oct 27 '22 17:10

Joe