douzhuangna6906 2014-09-16 16:23
浏览 48
已采纳

ajax帖子无法重定向到操作

Javascript:

$("#containerr").on('click', '#select', function() {
        data = {};
        val = {
               "name": 'John',
               "id": 10,
                }
        data.user = JSON.stringify(val);
        data.token = $("input[name=token]").val();
        console.log(data);
        url = '/test/ajax/';
        $.ajax({
            url: url,
            type: "POST",
            data: data,
            success: function(data) {
                alert('success');
            },
            error: function(xhr, ajaxOptions, error) {
                alert(xhr.status);
                alert(error);
            },
        });
        return true;
    });

PHP:

public function ajax()
{
       $input = $this->input->post();
       print_r($input['user']);
}

HTML:

<div id="container">
       <input type="hidden" name="token" value="8sdf243dfa426b2sfwe434fdg43gwsf" />
       <button type="submit" id="select">Select</select>
</div>

Description:

When i click the select button, i see the the result of the post action. The print_r($input) in the php function also returns the desired result. However, as i am not using a form to perform this submission, the browser does not redirect to the post action (/test/ajax/ in this case). I can see the redirect occuring on the firebug console, but the browser does not actually redirect to that link. When i try window.location = 'http://www.mybaseurl.com/'+url, i cannot access data anymore, i lose the data. How can i redirect the browser to the post action url and still retain the data?

Edited javascript:

$("#container").on('click', '#select', function() {
            data = {};
            val = {
                   "name": 'John',
                   "id": 10,
                    }
            data.user = JSON.stringify(val);
            data.token = $("input[name=token]").val();
            console.log(data);
            url = '/test/ajax/';
            $.post(url, data, function(data) {
            alert('success');
            });
            return true;
        });
  • 写回答

1条回答 默认 最新

  • douzhan1031 2014-09-16 16:31
    关注

    The entire point of AJAX is that it doesn't redirect to the URL you define, it just sends data to it. If you want the user to go to the URL after the AJAX call is finished, you may not want to use AJAX. Just use a regular form.

    <form method="POST" action="/test/ajax">
      <input type="hidden" name="token" value="8sdf243dfa426b2sfwe434fdg43gwsf" />
      <button type="submit" id="select">Select</select>
    </form>
    

    edit:

    With the data you're sending, the form would actually look like

    <form method="POST" action="/test/ajax">
      <input type="text" name="name" />
      <input type="text" name="id" />
      <input type="hidden" name="token" value="8sdf243dfa426b2sfwe434fdg43gwsf" />
      <button type="submit" id="select">Select</select>
    </form>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 dbLinq最新版linq sqlite
  • ¥20 对D盘进行分盘之前没有将visual studio2022卸载掉,现在该如何下载回来
  • ¥15 完成虚拟机环境配置,还有安装kettle
  • ¥15 2024年全国大学生数据分析大赛A题:直播带货与电商产品的大数据分析 问题5. 请设计一份优惠券的投放策略,需要考虑优惠券的数量、优惠券的金额、投放时间段和投放商品种类等因素。求具体的python代码
  • ¥15 有人会搭建生鲜配送自营+平台的管理系统吗
  • ¥15 用matlab写代码
  • ¥30 motoradmin系统的多对多配置
  • ¥15 求组态王串口自定义通信配置方法或代码?
  • ¥15 实验 :UML2.0 结构建模
  • ¥20 用vivado写数字逻辑实验报告撰写,FPGA实验