猿问

ajax post 返回的数据是null

因为form提交的时候页面会自动提交到/apply 这个处理页面,返回JSON数据

所以用ajax重定向到/applysuccess

重定向成功了可是数据怎么没提交成功的样子 获取的时候看到都是null

求帮助


//ajax的代码

 $.ajax({

        type: 'post',

        url: '/apply',

        async: true,

        dataType: 'json',

        data: $('#sub').serialize(),

        success: function (data) {


            $("#sub").attr("method", 'GET').attr("action", '/applysuccess');

            $('#apply').html('<h3>' + data.message + '</h3>');

            $('.count').html(' ' + data.count + ' ');

        }

    });

<!--这是表单的代码-->

<form action="/apply" method="post" id="sub">


                <div class="form-group fl col-md-4">

                    <input class="form-control" name="first_name" placeholder="first name" required>

                    <input class="form-control" name="last_name" placeholder="last name" required>

                </div>

                <div class="form-group fl col-md-4">

                    <input class="form-control" name="position_id" placeholder="position id" required>

                    <input class="form-control" name="years_experience" placeholder="years experience" required>

                </div>

                <div class="form-group fl col-md-4">

                    <input class="form-control" name="expertise" placeholder="expertise" required>

                    <button id="applyBtn" type="submit" class="btn btn-primary btn-lg">Apply for Job NOW</button>

                </div>

            </form>

请输入代码


慕哥9229398
浏览 914回答 1
1回答

慕田峪7331174

更新:<form id="form" ...>&nbsp; &nbsp; ...&nbsp; &nbsp; <input type="submit" value="提交"/></form><script>&nbsp; &nbsp; document.querySelector('#form').onsubmit = e => {&nbsp; &nbsp; &nbsp; &nbsp; // 你有修改 action 的做法,所以这里可能需要判断一下现在 acion 的值然后做不同的操作&nbsp; &nbsp; &nbsp; &nbsp; const action = $('#suib').attr('action')&nbsp; &nbsp; &nbsp; &nbsp; if (action === '/apply') {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $.ajax({&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; type: 'post',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; url: '/apply',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; async: true,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dataType: 'json',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; data: $('#sub').serialize(),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; success: function (data) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $("#sub").attr("method", 'GET').attr("action", '/applysuccess');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $('#apply').html('<h3>' + data.message + '</h3>');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $('.count').html(' ' + data.count + ' ');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; &nbsp; &nbsp; } else if (action === '/applysuccess') {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // TODO ...&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; // 阻止表单的默认行为&nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; }</script>你 ajax 里都没传要 post 的数据啊。
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答