18

I've looked around and tried many different methods, but can't seem to pass actual data to my controller's function.

Here is some code:

        var URL = "/Timesheet/Timesheet/UpdateEntry";

        var dataObject = { 'newWeekEntry': newEntry, 'oldWeekEntry': oldEntry };

        alert(JSON.stringify(dataObject));

        $.ajax({
            url: URL,
            type: 'PUT',    
            data: JSON.stringify(dataObject),
            dataType: 'json',
            success: function(result) {
                alert("success?");
            }
        });

newEntry and oldEntry are both objects.

The alert line outputs this (with some properties removed, just for brevity):

{"newWeekEntry":{"MondayHours":2,"TuesdayHours":2,"WednesdayHours":5,"ThursdayHours":5,"FridayHours":"4","SaturdayHours":0,"SundayHours":0},"oldWeekEntry":{"MondayHours":2,"TuesdayHours":2,"WednesdayHours":5,"ThursdayHours":5,"FridayHours":2,"SaturdayHours":0,"SundayHours":0}}

When I debug my controller action ("UpdateEntry"), the two parameters are filled with the TimesheetEntry class default parameters (0).

Am I passing this in properly?

3
  • Can you expand on what you mean when you say "When I debug my controller action ("UpdateEntry"), the two parameters are filled with the TimesheetEntry class default parameters (0)."? What are you using on the server (i.e. Rails) and how are you converting JSON on the server to TimesheetEntry? Commented Oct 24, 2012 at 20:08
  • 2
    The dataType attribute is only used when you're getting data from the server. You should be setting contentType to application/json when sending data to the server. Commented Oct 24, 2012 at 20:11
  • Thank you InPursuit! That fixed it. Would you like to add this as an answer and I can accept it? Commented Oct 24, 2012 at 20:13

3 Answers 3

40

The dataType attribute is only used when you're getting data from the server. You should be setting contentType to application/json when sending data to the server.

Sign up to request clarification or add additional context in comments.

Comments

7
$.ajax({
        url: window.serverUrl + 'student/event/' + eventId,
        type: 'put',
        data: JSON.stringify(data),
        headers: {
            'x-auth-token': localStorage.accessToken,
            "Content-Type": "application/json"
        },
        dataType: 'json'
})

This worked for me

Comments

4

Use headers: {"X-HTTP-Method-Override": "PUT"} and override the POST request type. It works on my project...

$.ajax({
    type: 'POST', // Use POST with X-HTTP-Method-Override or a straight PUT if appropriate.
    dataType: 'json', // Set datatype - affects Accept header
    url: "http://example.com/people/1", // A valid URL
    headers: {"X-HTTP-Method-Override": "PUT"}, // X-HTTP-Method-Override set to PUT.
    data: '{"name": "Dave"}' // Some data e.g. Valid JSON as a string
});

5 Comments

Why use 'POST' as the type but override it in the headers?
Look up and you will see that put as a type doesn't work as should be
And how to get the in your controller?
This only works with those servers that understand that header, as a workaround for lacking support for the 'PUT' or 'DELETE' methods. It's awful.
Sorry, I should say that it's awful, as it introduces security concerns. If you can legitimately and properly do a PUT request, do so.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.