jQuery的阿贾克斯没有得到张贴到PHP文件 [英] jQuery Ajax is not getting posted to a PHP file

查看:125
本文介绍了jQuery的阿贾克斯没有得到张贴到PHP文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的AJAX,所以我知道有可能是我的code一个愚蠢的错误,但不管怎样,我会继续前进。

I am new to AJAX so I know there could be a silly mistake in my code but anyways I will go ahead.

我创建了一个功能,一个按钮被点击时调用。该函数调用的jQuery的阿贾克斯()方法。我将数据发送到一个名为delete_post.php`文件。

I have a function created that is called when a button is clicked. The function calls .ajax() method of jquery. I send the data to a file named 'delete_post.php`.

HTML:

<button class="btn btn-primary" onclick="deletePost(<?php echo $_GET["id"]; ?>);">Yes</button>

以上code工作。

The above code works.

JS:

function deletePost(postid) {
    $.ajax({
        type: 'post',
        url: "delete_post.php?id="+postid,
        success: function(data) {
            if(data.error == true) console.log('error');
                    else console.log('problem?');
        }
    });
}

以上code被调用阿贾克斯()的功能,但不记录的问题?到控制台。

The above code is calling the .ajax() function but is not logging 'problem?' into the console.

下面是PHP文件:

<?php
require_once '...';
if(isset($_GET["id"])) {
    $id = $_GET["id"];
    echo "YEAH!";
} else {
    header("location: index.php");
}
?>

有什么问题,我该如何解决?

What's the issue and how can I fix it?

推荐答案

正如我们在聊边讨论,你可以使用这样的:

as we discussed in the chat side, you can use it like this:

记者:

function deletePost(postid) { 
$.post('delete_post.php', {id : postid}, function(data){ 
console.log(data); 
}, 'json'); 
}

PHP:

 <?php 

    require_once '...'; 
    if(isset($_POST["id"])) { 
    $data['res'] = 'yes'; 
    echo json_encode($data); 
    } else { 
    header("location: index.php"); 
    } 
    ?> 

这篇关于jQuery的阿贾克斯没有得到张贴到PHP文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆