0

I want to send a json-array with POST to a webserver. Everything works fine, except of the wrong contentType of my object.

A "each" loops through my form and adds a pair of values to to two items. Then it adds the two items to an array and gets the next pair:

var jsonArray = []

$(form).each(function() {
.....
        item = {};
        item["name1"] = value1;
        item["name2"] = value2;


        jsonString = JSON.stringify(item);

        jsonarray.push(jsonString); 
...

When i log the result to the console, everything looks great. The POST-method looks like this:

$.ajax ({
        type:"POST",
        contentType: 'application/json',
        url: siteRoot + "/" + ID + "/path",
        data: jsonarray,

...

But i am getting an error message from the server which is saying:

Content-type header is "application/json" but body is not a parseble JSON.

Which contentType should i use? If i look through the log file, i am getting output like this:

{"name1":"value1", "name2":"value2"}, {"name1":"value1", "name2":"value2"},...

Do i have to create a special JSON-Object?

2 Answers 2

2

The error message is correct because your output isn't a valid JSON string but a string containing JSON strings separated by semicolon. I suggest you serialize the entire array instead of single items. First build your array like this:

var itemsArray = []

$(form).each(function() {
...
    item = {};
    item["name1"] = value1;
    item["name2"] = value2;

    itemsArray.push(item); 
...

Then POST it like this:

$.ajax ({
    type:"POST",
    contentType: 'application/json',
    url: siteRoot + "/" + ID + "/path",
    data: JSON.stringify(itemsArray),
...

If you look at your content it should look like this:

[ {"name1":"value1", "name2":"value2"}, {"name1":"value1", "name2":"value2"}, ... ]

Which is valid JSON and should deserialize into an array of objects.

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

4 Comments

Many many thanks! But no i'm getting this error message: No validator found for real data media type 'application/json' and expected data media type 'text/plain'. Is it the server/API or yet another problem i my code?
@2DD8847 Most likely this is server side error but can't tell for sure as you haven't specified anything about your server side mechanism in the question, just the client side.
the server is provided, i haven't any access to that.
@2DD8847 In that case there isn't much more I can help you with, above answer allows you to send proper JSON, why server is not handling it is beyond the scope of this question I'm afraid.
1

The problem is with converting item to JSON and adding to array. This causes quotes around {}. like:

["{"name1":"value1","name2":"value2"}"]  // Not Valid

You should add item to array and then convert it

[{"name1":"value1","name2":"value2"}] //valid

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.