简体   繁体   English

将JavaScript对象转换为JSON并将其POST

[英]Convert JavaScript object to JSON and POST it

I have to parse my html from and POST it to another script. 我必须解析我的html并将其发布到另一个脚本。 When I use JSON.stringify to serialize object with parsed data, $_POST array in the receiving script is empty: 当我使用JSON.stringify来序列化带有解析数据的对象时,接收脚本中的$ _POST数组为空:

$("#addQueryForm").submit(function(event){
  event.preventDefault();
  result = {}     
  result['kindArr'];
  result['factor'];
  $("[rel=my-form]").each(function() {
    result[$(this).attr("name")] = $(this).attr("value");
  }); 
  var form = JSON.stringify(result);    
  $.post("add_kind.php", form , function(data) {
    alert(data);  
    //data shows me that $_POST array is empty
  }); 
});

But if I write json string into the query manually, it would be correct: 但是,如果我手动将json字符串写入查询,它将是正确的:

$.post("add_kind.php", {"kind":"Var1","kindArr":"Var12345","factor":"Var0","synonym1":"Var1","synonym2":"Var2","synonym3":"Var3"} , function(data) {
    alert(data);  
    //data shows me that $_POST contains posted data
});

What am I doing wrong? 我究竟做错了什么?

PS: stringify was excess. PS: stringify过剩了。

Maybe serialize would be better in your situation: 也许序列化会在你的情况下更好:

var form = $(this).serialize();    
$.post("add_kind.php", form, function(data) {
    alert(data);
});

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

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