4

I have this data structure:

var formValues = {
        TemporaryToken: a.userStatus.get("TemporaryToken"),
        MemorableWordPositionAndValues:[
            {
                Position: a.userStatus.get("MemorableWordPositions")[0],
                Value: this.$('[name="login-memorable-character-1"]').val()
            },
            {
                Position: a.userStatus.get("MemorableWordPositions")[1],
                Value: this.$('[name="login-memorable-character-2"]').val()
            },
            {
                Position: a.userStatus.get("MemorableWordPositions")[2],
                Value: this.$('[name="login-memorable-character-3"]').val()
            }
        ]
    }

And when I send it with $.ajax like so:

$.ajax({
        url:url,
        type:'PUT',
        //dataType:"json",
        data: JSON.stringify(formValues),
        success:function (data) {

        }
    });

It sends the request. However, if I send it like so:

$.ajax({
        url:url,
        type:'PUT',
        dataType:"json",
        data: formValues,
        success:function (data) {

        }
    });

I receive a 400 Bad Request. Is this a problem on the server or is JSON.stringify doing something different to just setting the dataType to 'json'?

3 Answers 3

3

The server is expecting a JSON string, not form parameters. JSON.stringify converts your form parameters object/array into a JSON string, which is what your server appears to be expecting.

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

4 Comments

How the F do you know it's going to ASP??
I don't, i made an assumption due to the number of ASP questions that come here with the same problem, Was i completely off? Whether it's ASP or not, the simple fact is the server rejects it with a 400 response code if it isn't JSON according to your question.
ha, excellent. Well AFAIK it's going to a C# API, but the guys do use ASP as well!
Updated to a non-server language specific answer, :-)
3

It is still sending the request in the second attempt; it is simply that your server does not understand the request. That is because jQuery automatically processes the formValues data into a query string before sending the data. See the documentation:

data

Type: Object, String

Data to be sent to the server. It is converted to a query string, if not already a string. It's appended to the url for GET-requests. See processData option to prevent this automatic processing. Object must be Key/Value pairs. If value is an Array, jQuery serializes multiple values with same key based on the value of the traditional setting (described below).

So, you must use JSON.stringify(), or not use JSON. Note that setting processData to false won't help, since it will simply send the string [object Object] to your server. See also this question.

Comments

0

What about $.param()?

var my_data = {};
my_data.post_name = 'post_value';
$.ajax({
    url:'/post_url.php'
    data:$.param(my_data);
    ...
});

Comments

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.