簡體   English   中英

使用ajax在對象中發布Json對象

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

我試圖通過此調用將新創建的數據發布到我的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);
});}

我想保存它像圖片中的頂部一樣,但它保存就像底部一個在那周圍嗎? 保存的json的圖像

我已經嘗試創建一個變量並將adres放在那里,但它不起作用。 感謝您的幫助

顯示您的服務器代碼。 如果您使用PHP,那么您可以嘗試它

$data = json_encode($_POST);

將數組轉換為json字符串

請參閱此答案: https//stackoverflow.com/a/5571112/3432422

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

我想保存它像圖片中的頂部一樣,但它保存就像底部一個在那周圍嗎?

您應該指定Ajax標頭,例如:

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

之后,每個ajax調用將使用特定於JSON的標頭發送。 要發送數據,您應該執行以下操作:

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

如果你在你的后端使用PHP,那將是不夠的,因為PHP無法使用JSON后期數據,而你的$ _POST將是空的,但如果你正在使用別的東西(java with spring,node js ..它等應該正確地解析數據。 在php的情況下,請看這里 - PHP:file_get_contents('php:// input')返回JSON消息的字符串

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM