weixin_33675507 2014-01-29 23:59 采纳率: 0%
浏览 85

Ajax serialize()表单错误

There have been similar questions, but I have browsed them a lot and found no accurate answer or fix so please offer a solution!

I am ajaxing a form to a page, and expecting a value back - no big deal. I've done it a million times before, but now it just refuses to work for this one form.

form html:

<form class="selectClaimType" action="place.php" method="post">
   <select id="claimtype" name="claimtype">
   <option value="privatebuilding">Private Building</option>
   <option value="communalbuilding">Communal Building</option>
   <option value="outside">Stored Outside</option>
   </select>
   <input type="submit" id="submit2" name="submit2" class="button" value="save"/>
</form>

jQuery:

$('form.selectClaimType').on('submit',function(e) {
    console.log('found');
    $form = $(this);
    console.log($form.serialize());
    e.preventDefault();
    $.ajax({
        url: "place.php", //$form.attr('action'),
        type: "post", //$form.attr('method'),
        data: $form.serialize(),
        success: function(data) {
            console.log(data);
            if (data == 1) {
                console.log('hello');
            }
            else {
                console.log('failure to change claim type'+data);
            }
        },
        data: function(data) {
            console.log('error ajaxing'+data);
        }
    });
});

The form is not dynamically created, and as you can see I have console.log(ged) nigh on everything. So I know that the form.serialize() is working (values appear as expected). I left out the preventDefault() to test, and the get values were correct. I have tried dataTypes of script, html, text, xml and json - no success.

I have a var_dump of $_REQUEST and $_POST on the posted to page - these are both empty arrays. I have changed the page that the post is sent to - still doesn't work.

Any ideas at all?

  • 写回答

3条回答 默认 最新

  • weixin_33719619 2014-01-30 00:02
    关注

    Maybe it is failing. Have you added a fail() callback to see if you get any output from that. Maybe the error is where you are submitting.

    评论

报告相同问题?