简体   繁体   English

使用ajax在对象中发布Json对象

[英]posting an Json object in an object using ajax

I am trying to post newly created data to my json database with this call 我试图通过此调用将新创建的数据发布到我的json数据库

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 保存的json的图像

I have already tried creating a variable and placing the adres in there but it did not work. 我已经尝试创建一个变量并将adres放在那里,但它不起作用。 Thank you for helping 感谢您的帮助

Show your server code. 显示您的服务器代码。 If you use PHP then you can try it 如果您使用PHP,那么您可以尝试它

$data = json_encode($_POST);

to convert array to json string 将数组转换为json字符串

See this answer: https://stackoverflow.com/a/5571112/3432422 请参阅此答案: https//stackoverflow.com/a/5571112/3432422

The data parameter should be a stringified object, like this: data参数应该是一个字符串化的对象,如下所示:

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);
    });}

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: 您应该指定Ajax标头,例如:

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

After it each ajax call will be send with JSON-specific headers. 之后,每个ajax调用将使用特定于JSON的标头发送。 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. 如果你在你的后端使用PHP,那将是不够的,因为PHP无法使用JSON后期数据,而你的$ _POST将是空的,但如果你正在使用别的东西(java with spring,node js ..它等应该正确地解析数据。 In case of php, look here - PHP: file_get_contents('php://input') returning string for JSON message 在php的情况下,请看这里 - PHP:file_get_contents('php:// input')返回JSON消息的字符串

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM