最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - how to submit form as JSON object - Stack Overflow

programmeradmin0浏览0评论

what I am doing is creating a form using JSON this form can then be edited a and produce new JSON object. The problem I am having seems to be with getting the form id. The code I am using to return a JSON object is:

form = document.forms[0];
$.fn.serializeObject = function()
{
    alert("start serializeObject");
    var o = {};
    var a = this.seralizeArray();
    $.each(a, function(){
        if (o[this.name] !== undefined) {
            if (!o[this.name].push) {
                o[this.name] = [o[this.name]];
            }
            o[this.name].push(this.value || '');
        } else {
            o[this.name] = this.value || '';
        }
    });
    return o;
    alert(o);
};

$(function() {
    alert("here");
    form.submit(function(){
        result.append(JSON.stringify(form.serializeObject()));
        return false;
    });
});

This just refresh's the pageI am not sure why. This program is not on a server and not be used on a server. by this I mean It is only every going to be run locally on a local machine, with no apache2 setup.

Thanks.

what I am doing is creating a form using JSON this form can then be edited a and produce new JSON object. The problem I am having seems to be with getting the form id. The code I am using to return a JSON object is:

form = document.forms[0];
$.fn.serializeObject = function()
{
    alert("start serializeObject");
    var o = {};
    var a = this.seralizeArray();
    $.each(a, function(){
        if (o[this.name] !== undefined) {
            if (!o[this.name].push) {
                o[this.name] = [o[this.name]];
            }
            o[this.name].push(this.value || '');
        } else {
            o[this.name] = this.value || '';
        }
    });
    return o;
    alert(o);
};

$(function() {
    alert("here");
    form.submit(function(){
        result.append(JSON.stringify(form.serializeObject()));
        return false;
    });
});

This just refresh's the pageI am not sure why. This program is not on a server and not be used on a server. by this I mean It is only every going to be run locally on a local machine, with no apache2 setup.

Thanks.

Share Improve this question edited Jun 19, 2012 at 10:43 Sagarmichael asked Jun 19, 2012 at 10:26 SagarmichaelSagarmichael 1,6247 gold badges24 silver badges54 bronze badges 3
  • 1 You have quite a lot of typos in your code. from, seralize – ThiefMaster Commented Jun 19, 2012 at 10:29
  • This program is not on a server and not be used on a server. means? Please write clearly the requirement..if u want to submit data as json then check jquery ajax {post:'json'} example.. – Rajesh Commented Jun 19, 2012 at 10:29
  • The page is just refreshing, probably, because your html code has a submit button, and a form with the action and method. This way html is trying to submit. Next time post your html also – Lombas Commented Sep 3, 2015 at 22:06
Add a comment  | 

2 Answers 2

Reset to default 10

You code can be written pretty easy. This is how I do it:

Ajax:

$('#formID').on('submit',function () {
    $.ajax({
        url: 'submit.php',
        cache: false,
        type: 'POST',
        data : $('#formID').serialize(),
        success: function(json) {
            alert('all done');
        }
    });
});

If you are not sending it with Ajax, why would you do this? If you are simply submitting the form, you can do it using PHP like this:

<?php
$json_object = json_decode($_POST);
?>
$('#formID').on('submit',function (e) {
    e.preventDefault();
    $.ajax({
        url: 'submit.php',
        cache: false,
        type: 'POST',
        data : $('#formID').serialize(),
        success: function(json) {
        alert('all done');
    }
    });
});

if you want not redirect or refresh use e.preventDefault();

发布评论

评论列表(0)

  1. 暂无评论