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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度