简体   繁体   English

如何在ajax javascript中以json格式将表单数据发送到服务器

[英]how to send the form data to server in json format in ajax javascript

function ajax() {
  $('form').submit(function() {
    console.log($(this).serializeArray());
    $('#result').text(JSON.stringify($(this).serializeArray()));
    return false;
  });
}

This is the json data i'm getting: 这是我得到的json数据:

[{"name":"firstName","value":"erere"},{"name":"lastName","value":"rere"},{"name":"emailAddress","value":"eregedfd@gmail.com"},{"name":"password","value":"dfdfd"},{"name":"phoneNumber","value":"989989898"}]

How can I send it to the server. 如何将其发送到服务器。 What should I include in data in the ajax call? 我应该在ajax调用中包含哪些数据?

A simple example: 一个简单的例子:

$('form').submit(function() {

    $.post( "send.php", $(this).serializeArray())
      .done(function( reply ) {
        alert( "Complete, reply from server: " + reply );
      });

    return false;
});

See: http://api.jquery.com/jquery.post/ for information on handling callbacks. 有关处理回调的信息,请参阅: http//api.jquery.com/jquery.post/

Try this: 试试这个:

$('form').submit(function() {
    var form = $(this);
    var data = form.serialize();

    $.ajax({
        url: 'post url'
        method: 'POST',
        data: data,
        success: function(resp){
             //action on successful post
        },
        error: function() {
            //handle error
        }
    });
    return false;
});

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

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