weixin_33670786 2015-04-02 22:28 采纳率: 0%
浏览 8

Ajax提交不起作用

I'll make it easy, I want to submit data without using a form, etc, etc, etc... I have this code:

HTML

<span class="categ-edit">Edit</span>
<span class="categ-add">Add</span>
<span class="categ-delete">Delete</span>

JQUERY

$('.categ-edit').click(function() {
    $.ajax({
    url: 'categoryactions.php',
    type: 'POST',
    data: {action: 'edit'},
    });
    window.location.href = "categoryactions.php";
});

$('.categ-add').click(function() {
    $.ajax({
    url: 'categoryactions.php',
    type: 'POST',
    data: {action: 'add'},
    });
    window.location.href = "categoryactions.php";
});

$('.categ-delete').click(function() {
    $.ajax({
    url: 'categoryactions.php',
    type: 'POST',
    data: {action: 'delete'},
    });
    window.location.href = "categoryactions.php";
});

And in categoryactions.php I have this:

PHP

<?php

    $action = $_POST['action'];
    echo $action;

?>

But when it redirects me to categoryactions.php I get nothing. I'm not sure if that's the way to submit data with AJAX but at least I tried. If someone knows how to fix this, I'll be grateful!

  • 写回答

4条回答 默认 最新

  • weixin_33716557 2015-04-02 22:33
    关注

    You click handler is making two separate requests. First, you are sending a request with AJAX, then you are going to the page. When you look at the page, you won't see the result because the result was given to the AJAX request.

    The point of AJAX is to avoid changing the page.

    Try this:

    $('.categ-edit').click(function() {
        $.ajax({
            url: 'categoryactions.php',
            type: 'POST',
            data: {action: 'edit'},
            success: function(data) {
                alert(data);
            }
        });
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥15 宇视监控服务器无法登录
  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥50 power BI 从Mysql服务器导入数据,但连接进去后显示表无数据
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签天线)