简体   繁体   English

jQuery Ajax对php的调用未执行

[英]JQuery Ajax call to php not executing

I'm creating a basic website with a button that, when clicked, makes a jquery Ajax call to a php script that echoes something back. 我正在创建一个带有按钮的基本网站,当单击该按钮时,它将对回显某些内容的php脚本进行jquery Ajax调用。

things I've tried: 我尝试过的事情:

I've looked at Call php function from JavaScript to make sure I wasn't just making a syntax error, but that didn't help. 我查看了JavaScript中的Call php函数,以确保我不仅在语法上出错,但这没有帮助。

I've changed the dataType part of my jquery call to script , html , json , and text , but it just wound up returning the entire code in some cases (eg just alerting test.php without executing it) and throwing an error: 我已经将我的jquery调用的dataType部分更改为scripthtmljsontext ,但是在某些情况下,它只是结束了返回整个代码的过程(例如,仅警告test.php而不执行它)并抛出错误:

XML Parsing Error: no root element found XML解析错误:找不到根元素

Location: file:///Users/jonathansekela/Documents/Projects/sz12-library/php/test.php 位置:file:///Users/jonathansekela/Documents/Projects/sz12-library/php/test.php

Line Number 9, Column 3 第9行,第3列

in the case of json , the error: json的情况下,错误:

error: parsererror: SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data 错误:parsererror:语法错误:JSON.parse:JSON数据第1行第1列的意外字符

From what I can tell, whenever I try to get jQuery to execute some PHP with a simple Ajax call, it doesn't execute the PHP, instead simply treating it like pure xml and returning the entire thing as a string back to my function without doing any of the php. 据我所知,每当我尝试通过简单的Ajax调用让jQuery执行某些PHP时,它都不会执行PHP,而是将其视为纯xml并将整个内容作为字符串返回给我的函数,而无需做任何的PHP。

The end-goal: 最终目标:

when pressed, the button in the html will call a js function that makes an Ajax call to a php script. 当按下时,html中的按钮将调用js函数,该函数对php脚本进行Ajax调用。 The php script then connects to a mysql database, does some stuff, returns, some other stuff, and gives that other stuff back to the jQuery call so it can put some results back on the html screen. 然后,PHP脚本连接到mysql数据库,执行一些操作,返回,其他操作,并将其他操作返回给jQuery调用,以便可以将一些结果返回到html屏幕上。 Literally basic proof-of-concept level stuff, except for the one part where my computer doesn't think that the php is actually php. 从概念上讲是基本的概念验证级别的东西,除了我的计算机认为php实际上不是php的那一部分。

What am I missing here? 我在这里想念什么? Any and all suggestions welcome. 任何和所有建议,欢迎。 I will add any extra information if requested. 如果需要,我将添加任何其他信息。

index.html: index.html:

<div class = "row">
    <div class="col-xs-12 btn btn-default button" onclick="test();">
        test ajax
    </div>
</div>

js/index.js: js / index.js:

var test = () => {
    $("#button-text").html("test clicked");
    $.ajax({
        type: "POST",
        data: {
            user: 1,
            first: "jonny",
            last: "test"
        },
        url: "php/test.php",
        dataType: "html",
        success: function(result) {
            alert("success: "+result);
    },
    error: function(xhr, status, error) {
        alert("error: "+status + ': ' + error);
    }
    });
}

php/test.php: php / test.php:

<?php
function test() {
    $user = $_POST['user'];
    $first = $_POST['first'];
    $last = $_POST['last'];
    echo "your data: ".$user." + ".$first." + ".$last;
}
test();
?>

The answer, as explained by the amazing Kakurala : I need to put my code on a server with a LAMP/XAMP stack running before I can call PHP. 答案由惊人的Kakurala解释:我需要将代码放在运行LAMP / XAMP堆栈的服务器上,然后才能调用PHP。 So either setup a LAMP stack on my laptop for dev purposes or get a cloud server and set it up there (an AWS EC2 instance , for example). 因此,可以出于开发目的在我的笔记本电脑上设置LAMP堆栈,也可以获取云服务器并将其设置在那里( 例如AWS EC2实例 )。 Thanks a ton for your help, friend! 朋友,非常感谢您的帮助!

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

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