Search code examples
jqueryjsonajaxwcf-rest

Ajax POST: "Server encountered an error processing the request"


I have a jQuery Mobile app communicating to a WCF REST service. The majority of my calls to the REST service are GETs. However, one function uses a POST with JSON data.

The POST works perfect on our development servers, so I'm fairly certain it isn't my code. When I move it to production, it doesn't work any more. I've included some logging in the REST service, so I have a good idea what method is being called as the mobile app communicates. All the GETs go through fine, logging which method was used. But the POST method doesn't even get called.

When I send the POST request I get "The server encountered an error processing the request. See server logs for more details." I've looked all through the Event Viewer on the server and can't find any messages pertaining to this. We've also looked at the firewall logs, and I'm not seeing anything being blocked.

I'm at a loss as to what is causing the error.

Here's my Ajax call:

    var JSONObject = {
        "customerID": customerID
        , "keyserialNumber": serialNumber
        , "issuedToName": newKeyHolderName
        , "userID": loggedInUserID
        , "keyIssuedUserID": newKeyHolderUserID
        , "signature": signature
        , "userEmail": loggedInUserEmail
        , "ccEmails": ccEmails
    };

    $.ajax({
        url: baseUrl + 'UpdateKeyReceipt',
        type: 'POST',
        data: JSON.stringify(JSONObject),
        error: function(e){
            console.log(e);
        },
        dataType: 'JSON',
        contentType: 'application/json; charset=utf-8',
        success: function (data) {
            ClearLookupData();
            $('#popKeyReceipt').popup('close');
            $.mobile.loading('hide');
        }
    });
    $.mobile.loading('hide');

The REST service looks like this:

Interface:

    [OperationContract]
    [WebInvoke(Method = "POST",
       ResponseFormat = WebMessageFormat.Json,
       RequestFormat = WebMessageFormat.Json,
       BodyStyle = WebMessageBodyStyle.WrappedRequest,
       UriTemplate = "UpdateKeyReceipt")]
    string KeyReceiptData(Stream data);

Method called:

    public string KeyReceiptData(Stream data)
    {
        string logFile = @"\\ikraid\Clients\InstaKey\Logs\IKRestLog.txt";
        using (StreamWriter sw = File.AppendText(logFile))
        {
            sw.WriteLine("KeyReceiptData: ");
        }

        StreamReader reader = new StreamReader(data);

        string result = reader.ReadToEnd();
        reader.Close();
        reader.Dispose();

        UpdateKeyReceipt keyData = JsonConvert.DeserializeObject<UpdateKeyReceipt>(result);

        Helper.UpdateKeyReceipt(keyData.CustomerID.ToString(), keyData.KeySerialNumber.ToString(), keyData.IssuedToName.ToString(),
            keyData.UserID.ToString(), keyData.KeyIssuedUserID.ToString(), keyData.Signature.ToString(),
            keyData.UserEmail.ToString(), keyData.CCEmails.ToString());

        return "Received: " + result;
    }

The log file never is written to, so I know it's not even reaching the method.

Anyone have anything I can do, or look at that might help, I'd very much appreciate it.

Thanks


Solution

  • Finally figured this out. The problem was the line:

    contentType: 'application/json; charset=utf-8',

    I took that out of the ajax call and then was able to see progress in the log files. Then found an issue with the pdf creation in the rest service. Once the REST service was fixed, the app works like a charm.

    I'd originally put that in as I'd seen many solutions that said that line must be included. Hadn't thought to take it out to test.