This project is read-only.

$.ajax PUT and POST

Topics: Web Api
May 31, 2011 at 8:07 PM

Hi guys,

I'm trying to figure out how to execute a PUT and a POST from javascript.  In the WCF Web API download bundle, there is the ContactManager_Simple project.  In Default.htm, it uses $.getJSON() for a GET, $.post() for a POST, and $.ajax() with the DELETE type for a DELETE.

  1. What happened to PUT?  Is it just not supported?
  2. Using $.ajax() with the POST type does not seem to deserialize the Contact object correctly on the server.

    This is the call from the js:

        type: "POST",
        url: "contacts",
        data: data,
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        success: function () {

    This is the error that is thrown:

    The service operation 'Post' expected a value assignable to type 'Contact' for input parameter 'contact' but received a value of type 'HttpRequestMessage`1'.

    This is the "source file information":

    Locating source for 'F:\advisorsrepo\wcfhttp\Http\Src\Microsoft.ApplicationServer.Http\Microsoft\ApplicationServer\Http\Dispatcher\HttpMessageFormatter.cs'. Checksum: MD5 {c 48 e1 62 6d b5 af b2 15 6c e3 7c ef f0 d4 ec}
    The file 'F:\advisorsrepo\wcfhttp\Http\Src\Microsoft.ApplicationServer.Http\Microsoft\ApplicationServer\Http\Dispatcher\HttpMessageFormatter.cs' does not exist.
    Looking in script documents for 'F:\advisorsrepo\wcfhttp\Http\Src\Microsoft.ApplicationServer.Http\Microsoft\ApplicationServer\Http\Dispatcher\HttpMessageFormatter.cs'...
    Looking in the projects for 'F:\advisorsrepo\wcfhttp\Http\Src\Microsoft.ApplicationServer.Http\Microsoft\ApplicationServer\Http\Dispatcher\HttpMessageFormatter.cs'.
    The file was not found in a project.
    Looking in directory 'C:\Program Files\Microsoft Visual Studio 10.0\VC\crt\src\'...
    Looking in directory 'C:\Program Files\Microsoft Visual Studio 10.0\VC\atlmfc\src\mfc\'...
    Looking in directory 'C:\Program Files\Microsoft Visual Studio 10.0\VC\atlmfc\src\atl\'...
    Looking in directory 'C:\Program Files\Microsoft Visual Studio 10.0\VC\atlmfc\include\'...
    The debug source files settings for the active solution indicate that the debugger will not ask the user to find the file: F:\advisorsrepo\wcfhttp\Http\Src\Microsoft.ApplicationServer.Http\Microsoft\ApplicationServer\Http\Dispatcher\HttpMessageFormatter.cs.
    The debugger could not locate the source file 'F:\advisorsrepo\wcfhttp\Http\Src\Microsoft.ApplicationServer.Http\Microsoft\ApplicationServer\Http\Dispatcher\HttpMessageFormatter.cs'. 

What do I need to do to make these types of calls work properly?  I'd much rather use CLR objects on the server versus the JsonObject because a WCF service already exists and I'd really like to just use the existing code with very little adjustments to expose as a REST/HTTP service.  Also, the ultimate goal is to use custom basic authentication to protect the service so all calls must have the Authorization header which means using the $.post() jQuery method is out of the question, and there is no equivalent $.put() anyway.

Any help would be very much appreciated.  This is the last roadblock I'm dealing with to get the services working the way I want.  Everything else I've been able to get through via the samples and this forum with your feedback and the user feedback.  Thanks for getting me this far!!

Jun 1, 2011 at 4:31 PM

After some more research, the culprit is the contentType property of the json object in the $.ajax call.  If I remove the contentType line, POSTs and PUTs both work flawlessly!!  Again, thanks for the hard work to get this great framework!