简体   繁体   English

如何获取jQuery AJAX将.map函数var值发布为JSON字符串

[英]How to get jQuery AJAX to post .map function var value as JSON string

In example of option A below, the PHP script post result stated undefined when echo the post data. 在以下选项A的示例中,当回显发布数据时,PHP脚本发布结果声明为undefined But at the same time the console.log displays: [Object { set_capture="resize", set_unknown="unknown"}] . 但同时console.log显示: [Object { set_capture="resize", set_unknown="unknown"}] I would like to know What is wrong with the .map function or the ajax code in option A ? 我想知道选项A中的.map函数或ajax代码有什么问题吗? For option B, it works well after removing the .map function. 对于选项B,在删除.map函数之后,它可以很好地工作。 What am I missing ? 我想念什么?

HTML Form: HTML表单:

<form action="myphp.php" method="post" name="form-foo">
    <input type="radio" id="r1" class="set-img" name="set_capture" value="resize" />
    <input type="radio" id="r2" class="set-img" name="set_capture" value="original" />
    <input type="checkbox" id="r3" class="set-img" name="set_unknown" value="unknown" />
    <input type="submit" id="submit" name="submit" value="Save" />
</form>

Option A: 选项A:

$(function() {
  $("#submit").on("click", function() {
    var radioValues = $('#form-foo').map(function() {
      return {
        set_capture: $('input[name="set_capture"]:checked').val(),
        set_unknown: $('input[name="set_unknown"]:checked').val()
      };
    }).get();
    console.log(radioValues);
    //var formData = JSON.stringify(radioValues);
    var formData = radioValues;
    var formURL = $("#form-foo").attr("action");
    console.log(formData);
    $.ajax({
      url: formURL,
      type: "POST",
      data: formData,
      cache: false,
      dataType: "json"
    }).done(function(data) {
      // more codes
    }, "json");
    return false;
  });
});

Option B: 选项B:

$(function() {
  $("#submit").on("click", function() {
    var capture = $('input[name="set_capture"]:checked').val();
    var unknown = $('input[name="set_unknown"]:checked').val();
    var formData = {
      set_capture: capture,
      set_unknown: unknown
    };
    var formURL = $("#form-foo").attr("action");
    console.log(formData);
    $.ajax({
      url: formURL,
      type: "POST",
      data: formData,
      cache: false,
      dataType: "json"
    }).done(function(data) {
      // more codes
    }, "json");
    return false;
  });
});

.map is always return as array .Ajax data format is object {} So it will get undefined in your POST. .map始终以数组形式返回。Ajax数据格式为对象{}因此在POST中将变得不确定。 To solve that you need to add index 0 of formdata 要解决您需要添加formdata索引0

$.ajax({
      url: formURL,
      type: "POST",
      data: formData[0], //here add index 0 
      cache: false,
      dataType: "json"
    }).done(function(data) {
      // more codes
    }, "json");
    return false;
  });

Note : data is never pass undefined value to server post ,so if you didn't check your input or checkbox,It will post nothing... 注意:数据永远不会传递未定义的值到服务器发布,所以如果您不检查输入或复选框,它将什么也不会发布...

Ajax data must is array. Ajax data必须是数组。 EX:{"capture":123, "unknow":456}. EX:{“ capture”:123,“ unknow”:456}。

With Option A : formData is [Object] not array, so it not working. 使用选项AformData不是[Object]数组,因此它不起作用。

With my knowledge, sorry if this wrong! 据我所知,对不起,如果错了!

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

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