北城已荒凉 2016-03-25 01:30 采纳率: 0%
浏览 25

显示Json回应

I'm new to both symfony and ajax , i want to submit a form without refreshing the page everytime i was following along with this tutorial , everything working the page does submit to the database but the problem is that i get this message in the browser

{"message":"Success!"}

This is what i have in my controller .

$bon = new Bons();
    $form = $this->createForm('AppBundle\Form\BonsType', $bon);

    $form->handleRequest($request);

    if ($form->isSubmitted() && $form->isValid()) {
        $em = $this->getDoctrine()->getManager();
        $em->persist($bon);
        $em->flush();

        return new JsonResponse(array('message' => 'Success!'), 200);
    }

    return $this->render('bons/new.html.twig', array(
        'bon' => $bon,
        'form' => $form->createView(),
    ));

The ajax part :

$(document).ready(function() {
        $('body').on('submit', '.myForm', function (e) {

            e.preventDefault();

            $.ajax({
                type: $(this).attr('method'),
                url: $(this).attr('action'),
                data: $(this).serialize()
            })
            .done(function (data) {
                if (typeof data.message !== 'undefined') {
                    alert(data.message);
                }
            })
            .fail(function (jqXHR, textStatus, errorThrown) {
                if (typeof jqXHR.responseJSON !== 'undefined') {
                    if (jqXHR.responseJSON.hasOwnProperty('form')) {
                        $('#form_body').html(jqXHR.responseJSON.form);
                    }

                    $('.form_error').html(jqXHR.responseJSON.message);

                } else {
                    alert(errorThrown);
                }

            });
        });
    });

is there any way to submit the form without redirecting or refreshing the page

  • 写回答

1条回答 默认 最新

  • 谁还没个明天 2016-03-25 01:35
    关注

    Turns out your problem was really simple. You just need to add the dataType like so:

    $.ajax({
        type: $(this).attr('method'),
        dataType: "json",
        url: $(this).attr('action'),
        data: $(this).serialize()
    })
    

    I want to show you what I normally do

    My php file looks like this:

    $json = array();
    $my_variable = $_POST('variable_name'); // or however I get the value
    if(something == something)
        $json['success'] = 'It was successful';
    else
        $json['error'] = 'Something went wrong';
    die(json_encode($json));
    

    My JS looks like this:

    $('.update_class').click(function(){
        var variable_I_am_sending = $(this).data('something');
        $.post('path_to_php', {variable_name: variable_I_am_sending}, function(data, result, xhr){
            if(result == 'success')
                if(data.success)
                    alert(data.success);
                else
                    alert(data.error);
            else
                alert('ajax call failed');
        }, "json");
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?