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

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵