使用 javascript POST 数组数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44003115/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 02:17:04  来源:igfitidea点击:

POST array data with javascript

javascriptarraysajaxhttppost

提问by J000S

I'm trying to post some data to a http connector but cant figure out how i can post Array data...

我正在尝试将一些数据发布到 http 连接器,但无法弄清楚如何发布数组数据...

My data Looks like that:

我的数据看起来像这样:

var data = [{ 
    key:'myKey',
    keyName:'myKeyName',
    value:'value',
    valueName:'valueName' 
}, { 
    key:'mySecondKey', 
    keyName:'mySecondKeyName',
    value:'secondValue',
    valueName:'secondValueName'
}, ...and so on ];

I try to post it via Ajax like this:

我尝试通过 Ajax 发布它,如下所示:

$.ajax({
    type:   "POST",
    url:    url,
    data:   data,
    error:  function(){
        alert("Error");
    },
    success: function(){
        alert("OK");
    }
});

The request Returns ok but when i look at the request data it Posts undefined=value&undefined=secondValue

请求返回正常,但是当我查看它发布的请求数据时 undefined=value&undefined=secondValue

How can i solve this? I Need to save all those informations for configurations

我该如何解决这个问题?我需要保存所有这些配置信息

A simple post with just some keys and values like key=value&key2=value2works like a charm.

一个只有一些键和值的简单帖子key=value&key2=value2就像一个魅力。

回答by Woodrow

I'm assuming you're making a POST request with a JSON payload. First, you want to make sure your payload is formatted JSON correctly, use: http://pro.jsonlint.com/

我假设您正在使用 JSON 有效负载发出 POST 请求。首先,您要确保您的有效负载格式正确,请使用:http: //pro.jsonlint.com/

Secondly, you can send the payload using JSON.stringify and you will want to set the contentType:

其次,您可以使用 JSON.stringify 发送有效负载,并且您需要设置 contentType:

data: JSON.stringify(data), contentType: "application/json; charset=utf-8"

数据: JSON.stringify(data), contentType: "application/json; charset=utf-8"

If you run this and look at your network tab in Dev Tools in Chrome, it will error out, but you will at least see the payload formatted to send to the server: http://prntscr.com/f8hf6s

如果你运行它并在 Chrome 中的 Dev Tools 中查看你的网络选项卡,它会出错,但你至少会看到格式化为发送到服务器的有效负载:http: //prntscr.com/f8hf6s

var data = [{
    "key": "myKey",
    "keyName": "myKeyName",
    "value": "value",
    "valueName": "valueName"
  },
  {
    "key": "mySecondKey",
    "keyName": "mySecondKeyName",
    "value": "secondValue",
    "valueName": "secondValueName"
  }
];

var url = "http://swapi.co/api/";

$.ajax({
  type: "POST",
  url: url,
  data: JSON.stringify(data),
  contentType: "application/json; charset=utf-8",
  dataType: "json",
  error: function() {
    alert("Error");
  },
  success: function() {
    alert("OK");
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>