为什么我的AJAX功能后不工作按一下按钮? [英] Why is my ajax post function not working with button click?

查看:129
本文介绍了为什么我的AJAX功能后不工作按一下按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想POST变量到我的 PHP 在使用AJAX的一个按钮被点击文件,但是当我检查我的 PHP 页面加载,我没有收到变量。

I am trying to post variables to my PHP file when a button is clicked using AJAX, but when I check my PHP page loads, I am not receiving the variables.

$(document).ready(function(){
    $("#qryBtn").click(function(){
        // post qry //
        var qry = query();
        $.ajax({
            type: "POST",
            url: "favourites.php",
            data: {
                qry: qry
            },
            success: function (html) {
                console.log (qry);
            }
        });
    });
});

其中,查询()只是创建一个数组的功能。

Where query() is just a function that creates an array.

error_reporting ( E_ALL );
ini_set ( 'display_errors', 1 );
$query = "";
$key = "";
function getQueries() {
    if (isset ( $_POST ['qry'] )) {
        $query = $_POST ['qry'];
        echo $query;
    } else {
        echo "Query Missing";
    }
}
getQueries ();
function saveQueries() {
    if (isset ( $_POST ['keyValue'] )) {
        $key = $_POST ['keyValue'];
        $arr = array_combine ( $key, $query );
    } else {
        echo "Key Missing";
    }
}
saveQueries ();

输出:

查询MissingKey缺少

Query MissingKey Missing

我已经用类似的方法将数据发布到PHP网页,但不使用点击一个按钮,是有什么我失踪?

I have used similar procedure to post data to PHP pages but not using a button click, is there a something I'm missing?

推荐答案

可能是因为你没有声明的内容类型。

May be it is because you are not declaring content type.

如下更改脚本。

$(document).ready(function(){
    $("#qryBtn").click(function(){
        // post qry //
        var qry = query();
        $.ajax({
            type: "POST",
            url: "favourites.php",
            data: {
                qry: qry
            },
            contentType: 'application/json',
            success: function (html) {
                console.log (qry);
            }
        });
    });
});

默认jQuery的AJAX内容类型是应用程序/ x-WWW的形式urlen codeD;字符集= UTF-8 ,而你正在发布的 JSON 您的数据格式,使你必须明确地告诉你的是发送Ajax JSON 通过声明内容类型为应用程序/ JSON内容

Default jquery ajax content type is application/x-www-form-urlencoded; charset=UTF-8 while your are posting your data in json format so that you have to explicitly tell ajax that your are sending json content by declaring content type as application/json

修改

然后发送数据字符串内容。试试下面的脚本。 (请注意,我已删除的contentType 选项。

Then send data as string content. Try following script. (please note I have removed contentType option.

$(document).ready(function(){
        $("#qryBtn").click(function(){
            // post qry //
            var _data= JSON.stringify({qry:query()}); 
            $.ajax({
                type: "POST",
                url: "favourites.php",
                data: _data,
                success: function (html) {
                    console.log (qry);
                }
            });
        });
    });

这篇关于为什么我的AJAX功能后不工作按一下按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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