how to properly invoke a POST method on a json enabled wcf ria services domain service using ria.js

Topics: jQuery, Web Api
Jan 27, 2012 at 5:29 PM

the wcf ria services toolkit has a jquery plugin that streamlines access to domain services exposing a json endpoint.

Using the ria.js library, ajax calls to a domain service's query methods look something like this:

$.dataSource( 
    { 
        bufferChanges: true, 
        serviceUrl: 'https://foo.org/foo/ClientBin/FooService.svc/', 
        queryName: 'GetFoos', 
        refresh: function (results) { 
            var foos = results; 
        } 
    }).refresh(); 

What is the correct way to make an ajax call to a method with side-effects such as an [Invoke] method or the Login method on a domain service that implements IAuthentication?

Here's how I'm currently invoking the login method on my IAuthentication domain service- using jquery directly:

$.ajax( 
    { 
        type: 'POST', 
        url: 'https://foo.org/foo/ClientBin/AuthenticationService.svc/json/Login', 
        contentType: "application/json", 
        data: JSON.stringify({ 
            userName: 'jeremy', 
            password: '123456', 
            isPersistent: false, 
            customData: null 
        }), 
        success: function (profile) { 
            var user = profile; 
        }, 
        dataType: 'JSON', 
        error: function (jqXHR, textStatus, errorThrown) { 
            var message = errorThrown; 
        } 
    }); 

This technique works but is not ideal because it returns a couple of arrays (included results and root results) which aren't rolled up into a nice neat object graph like they would be when using $.dataSource().

If it helps, this is what the domain service looks like- nothing special:

public class AuthenticationService : LinqToEntitiesDomainService<SecurityEntities>, IAuthentication<SecurityProfile> 
{ 
    public SecurityProfile Login(string userName, string password, bool isPersistent, string customData) 
    { 
    ... 
    } 
 
    ... 
} 

Any pointers are appreciated!

http://stackoverflow.com/questions/9025968/how-to-properly-invoke-a-post-method-on-a-json-enabled-wcf-ria-services-domain-s