简体   繁体   English

提交表单时将JavaScript对象添加到表单数据

[英]Add JavaScript object to form data while submit the form

The user has the option to perform several actions in the form, after action when he submits the form, needs to add a JavaScript object to form. 用户可以选择在表单中执行多个操作,在提交表单之后,用户需要在表单中添加一个JavaScript对象。 I tried multiple ways, in server data reached as [Object object] 我尝试了多种方法,以服务器数据作为[Object object]

JavaScript Object JavaScript对象

var damageitems = {'5b3b43285d57025915000002':
                  {action: "accept",damage_location_name:"Front Bumper", estimated_total_repair_cost: "993.72", estimated_total_repair_cost_currency:"USD"}
                  }

Script I tried 我试过的脚本

$( "form" ).submit(function( event ) {
        event.preventDefault();
        url = $(this).attr( "action" );
        var postData = $(this).serializeArray();
        postData.push({name: 'damage_items', value: damage_items});
        $.post(url, postData, function(){});
    });

Value reached in server 服务器中达到的值

{"utf8"=>"✓", "_method"=>"put", "authenticity_token"=>"uztc+G0fyTb8wH7D6HZAslTfxuJvFLP3UunjiRjWylk=", "damage_items"=>"[object Object]", "action"=>"batch_update", "controller"=>"damage_items", "claim_id"=>"5b3b41415d57026665000001"}

My aims submit the value as normal submit not ajax, the value in js variable reaches in the server as a hash with key damage_items . 我的目标是将值提交为普通提交而不是ajax,js变量中的值以具有关键damage_items的哈希值形式到达服务器。 Not prefer the way add it in form control value then submit 不喜欢将其添加到表单控件值然后提交的方式

Updating your script to the following will do the trick 将脚本更新为以下内容即可解决问题

$( "form" ).submit(function( event ) {
    event.preventDefault();
    url = $(this).attr( "action" );
    var postData = $(this).serializeArray();
    postData.push({name: 'damage_items', value: JSON.stringify(damage_items)});
    $.post(url, postData, function(){});
});

When you try to store the data in the form, it is converted into a string using toString method if it is not a string already. 当您尝试以表格形式存储数据时,如果它还不是字符串,则使用toString方法将其转换为字符串。 toString method of Object returns [object Object] and that is why you are getting it. Object的toString方法返回[object Object] ,这就是为什么要获取它的原因。

Converting it into a JSON string will prevent that. 将其转换为JSON字符串将防止这种情况。

The best way is to turn a JavaScript Object Literal into a JSON (see the differences here ). 最好的方法是将JavaScript Object Literal转换为JSON (请参阅此处的区别)。 Because It is easy for machines to parse and generate and most of server side languages have methods to parse it. 因为机器很容易解析和生成,并且大多数服务器端语言都有解析它的方法。 Also in http requests, like ajax, we must send strings. 同样在http请求中,例如ajax,我们必须发送字符串。

Fortunately we have a method in js to do that, and is using JSON.stringify() and a method to turn a JSON into JavaScript Object Literal using JSON.parse() 幸运的是,我们在js中有一个方法可以做到这一点,并且正在使用JSON.stringify()和使用JSON.parse()将JSON转换为JavaScript Object Literal的方法。

Then, in your code: 然后,在您的代码中:

 //JavaScript Object Literal var damageitems = {'5b3b43285d57025915000002': {action: "accept",damage_location_name:"Front Bumper", estimated_total_repair_cost: "993.72", estimated_total_repair_cost_currency:"USD"} } damageitems = JSON.stringify(damageitems); //JSON - It is easy for machines to parse and generate // we could put this in a request because it's a string, console.log(damageitems); 

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

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