简体   繁体   English

Php 文件不会加载 ajax 请求

[英]Php file won't load ajax request

I try query my database from javascript, using ajax request to my php file.我尝试使用对我的 php 文件的 ajax 请求从 javascript 查询我的数据库。 This is my code:这是我的代码:

<?php
include('db.php');
session_start();
?>

    <!doctype html>
    <html lang="en">

<body>
    <div class="container">
        <canvas id="myCanvas" width="600" height="100"></canvas>
        <script src="jquery-3.5.1.min.js"></script>
        <script src="trainPassFrequency.js"></script>
            ...

So I included jquery file into my folder, so that I can access it as shown above.所以我将 jquery 文件包含到我的文件夹中,以便我可以如上所示访问它。 Now my trainPassFrequency.js look like this:现在我的 trainPassFrequency.js 看起来像这样:

function parseData(data) {
    console.log(data);
}
function trainFrequency() {
    $.post('ajaxRequests/trainfrequency.php'), {}, function (data) {
        console.log(data);
        if (typeof data === 'object') {
            parseData(data);
        }
    }
}
trainFrequency();

where trainfrequency.php only returns (echo) output of the sql query.其中 trainfrequency.php 仅返回(回显) sql 查询的 output。 When I run this code, I never get through $.post('ajaxRequests/trainfrequency.php'), {}, function (data) { .当我运行这段代码时,我从来没有通过$.post('ajaxRequests/trainfrequency.php'), {}, function (data) { Can anyone see my mistake?谁能看到我的错误?

I see that the problem is in your JavaScript code syntax, here:我看到问题出在您的 JavaScript 代码语法中,这里:

$.post('ajaxRequests/trainfrequency.php'), {}, function (data) {
        console.log(data);
        if (typeof data === 'object') {
            parseData(data);
        }
    }

While it seems that there is no syntax error, it really looks like it is not what you wanted.虽然看起来没有语法错误,但看起来确实不是您想要的。 Notice this part of code: $.post('ajaxRequests/trainfrequency.php') you somehow added ending parenthesis very early, so that part of code should look like this:注意这部分代码: $.post('ajaxRequests/trainfrequency.php')你很早就添加了结束括号,所以这部分代码应该如下所示:

$.post('ajaxRequests/trainfrequency.php', {}, function (data) {
            console.log(data);
            if (typeof data === 'object') {
                parseData(data);
            }
 });

Since this is a jQuery method you want to wrap all passed arguments between () parenthesis.由于这是一个 jQuery 方法,因此您希望将所有通过的 arguments 括在 () 括号之间。

Edit:编辑:

Also I want to explain why you didn't get any error, since this mistake can happen to all of us, and code just silently fails (doesn't work the way you want) even if it's perfectly valid in terms of syntax.另外我想解释一下为什么你没有收到任何错误,因为这个错误可能发生在我们所有人身上,并且代码只是默默地失败(不能按照你想要的方式工作),即使它在语法方面完全有效。

What happened is that you called $.post method with just one argument, hence this line of code: $.post('ajaxRequests/trainfrequency.php') then you added following data object, and callback arguments which should be inside the $.post method (between parenthesis), but since you closed parenthesis after first argument of $.post method, everything else became not related to that method at all, and in JavaScript comma is a valid operator, so when you separate data constructs with a comma operator it actually returns the value of the rightmost operand, this operator just evaluates those operands from left to right.发生的事情是你只用一个参数调用了 $.post 方法,因此这行代码:$.post('ajaxRequests/trainfrequency.php') 然后你添加了以下数据 object,并回调 arguments 应该在 $. post 方法(在括号之间),但是由于您在 $.post 方法的第一个参数之后关闭了括号,因此其他所有内容都与该方法完全无关,并且在 JavaScript 中,逗号是有效的运算符,因此当您使用逗号分隔数据结构时运算符它实际上返回最右边的操作数的值,该运算符只是从左到右评估这些操作数。 So if you console.log this thing:所以如果你 console.log 这个东西:

console.log($.post('ajaxRequests/trainfrequency.php'), {}, function (data) {
            console.log(data);
            if (typeof data === 'object') {
                parseData(data);
            }
        });

You'll get this function definition back in the console (rightmost value returned):您将在控制台中返回此 function 定义(返回最右边的值):

function (data) {
                console.log(data);
                if (typeof data === 'object') {
                    parseData(data);
                }
            }

Since what you did was unintentional I think it's really important to understand what happens because as I discussed above this code is happily executed but the reason could be hard to understand without understand the comma operator, and comma operator is not very common in the code, and is mostly used in code minification, optimization tools to reduce code size and such.由于您所做的事情是无意的,我认为了解发生的事情非常重要,因为正如我上面讨论的那样,这段代码很高兴执行,但是如果不理解逗号运算符,原因可能很难理解,并且逗号运算符在代码中不是很常见,并且主要用于代码缩小、优化工具以减少代码大小等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM