1

I am trying to post newly created data to my json database with this call

function sendtovenaarData(url){
$.ajax({

    url: "http://localhost:3000/" + url,
    type: 'POST',
    data:
    {
        voornaam:  $("#voornaam").val(),
        achternaam:  $("#achternaam").val(),
        beroep: $("#beroep").val(),
        adres:{
            streetAddress: $("#streetAddress").val(),
            city: $("#city").val(),
            state: $("#state").val(),
            zip: $("#zip").val()
        },
        haardplaats: $("#haardplaats").val(),
        favoriete_quote: $("#favoriete_quote").val()
    }

}).done(function () {

    console.log("data added");
    location.reload();

}).fail(function (xhr, message, error) {
    console.log(xhr, message, error);
});}

an i want to save it like the top one in the picture but it saves like the bottom one is there anyway around this? image of saved json

I have already tried creating a variable and placing the adres in there but it did not work. Thank you for helping

3
  • Try using JSON.stringify before adding to data key Commented Dec 29, 2016 at 12:34
  • use JSON.stringify() Commented Dec 29, 2016 at 12:34
  • so instead of whatever data you are passing is it saving hello only ? Commented Dec 29, 2016 at 12:35

3 Answers 3

0

Show your server code. If you use PHP then you can try it

$data = json_encode($_POST);

to convert array to json string

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

Comments

0

See this answer: https://stackoverflow.com/a/5571112/3432422

The data parameter should be a stringified object, like this:

function sendtovenaarData(url){
    $.ajax({
        url: "http://localhost:3000/" + url,
        type: 'POST',
        data: JSON.stringify(
        {
            voornaam:  $("#voornaam").val(),
            achternaam:  $("#achternaam").val(),
            beroep: $("#beroep").val(),
            adres:{
                streetAddress: $("#streetAddress").val(),
                city: $("#city").val(),
                state: $("#state").val(),
                zip: $("#zip").val()
            },
            haardplaats: $("#haardplaats").val(),
            favoriete_quote: $("#favoriete_quote").val()
        })
    }).done(function () {
        console.log("data added");
        location.reload();
    }).fail(function (xhr, message, error) {
        console.log(xhr, message, error);
    });}

Comments

0

an i want to save it like the top one in the picture but it saves like the bottom one is there anyway around this?

You should specify Ajax headers, something like:

$.ajaxSetup({
 dataType: 'json',
 contentType: 'application/json; charset=utf-8'
});

After it each ajax call will be send with JSON-specific headers. To send data you should do this one:

$.ajax({

    url: "http://localhost:3000/" + url,
    type: 'POST',
    data:
    JSON.stringify({
        voornaam:  $("#voornaam").val(),
        achternaam:  $("#achternaam").val(),
        beroep: $("#beroep").val(),
        adres:{
            streetAddress: $("#streetAddress").val(),
            city: $("#city").val(),
            state: $("#state").val(),
            zip: $("#zip").val()
        },
        haardplaats: $("#haardplaats").val(),
        favoriete_quote: $("#favoriete_quote").val()
    })

}).done(function () {

    console.log("data added");
    location.reload();

}).fail(function (xhr, message, error) {
    console.log(xhr, message, error);
});}

If you are using PHP on your backend, it will be not enough, because PHP can't work with JSON post data, and your $_POST will be empty, but if you are using something else (java with spring, node js ... etc) it should parse data properly. In case of php, look here - PHP: file_get_contents('php://input') returning string for JSON message

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.