简体   繁体   English

如何向 PHP 后端发出带有参数的 AJAX POST 请求?

[英]How do I make an AJAX POST request with parameters to a PHP backend?

This is an update to a previous question marked as a duplicate ( How do I send an AJAX POST request with parameters to PHP? )这是对标记为重复的上一个问题的更新( 如何向 PHP 发送带有参数的 AJAX POST 请求?

I am sending an AJAX request to a PHP backend.我正在向 PHP 后端发送 AJAX 请求。

The Javascript functions to make and receive the request: Javascript 函数发出和接收请求:

var httpRequest;

function makeRequest(){
    httpRequest = new XMLHttpRequest();

    httpRequest.onreadystatechange = receiveResponse;
    httpRequest.open("POST", "ajax_handler.php", true);
    httpRequest.setRequestHeader('Content-Type', 'application/json');
    httpRequest.send(JSON.stringify({key:"value"}));
}

function receiveResponse() {
    if (httpRequest.readyState === XMLHttpRequest.DONE) {
        if (httpRequest.status === 200) {
            console.log(httpRequest.responseText);
        }
    }
}

The PHP backend ajax_handler.php to process the request: PHP 后端ajax_handler.php处理请求:

<?php
    $data = file_get_contents('php://input');
    echo $data;
?>

My original post was marked as a duplicate of this question .我原来的帖子被标记为这个问题的副本。 That page has no clear examples, though I was able to infer that I should use file_get_contents('php://input') instead of $_POST in the PHP backend, as shown.该页面没有明确的示例,尽管我能够推断出我应该在 PHP 后端使用file_get_contents('php://input')而不是$_POST ,如图所示。

Using the code shown, console.log(httpRequest.responseText) in the receiving function logs {"key":"value"} to the console.使用显示的代码,接收 function 中的console.log(httpRequest.responseText){"key":"value"}记录到控制台。 If I try to interpret this as an array in the PHP backend, however, with但是,如果我尝试将其解释为 PHP 后端中的数组

<?php
    $data = file_get_contents('php://input');
    echo $data["key"];
?>

the response logs { to the console instead of the expected value .响应记录{到控制台而不是预期value

In addition to the use of 'php://input' , several responses in the linked duplicate also use the json_decode() function in the PHP backend.除了使用'php://input'之外, 链接副本中的几个响应还使用 PHP 后端中的json_decode() function。

If I include this function in the backend, however, as但是,如果我在后端包含此 function ,则为

<?php
    $data = json_decode(file_get_contents('php://input'));
    echo $data;
?>

The entire AJAX call breaks.整个 AJAX 调用中断。 It was set to trigger on a window click, but after including json_decode() , nothing happens on a window click, nothing is returned from the call, and nothing is logged to the console.它被设置为在 window 单击时触发,但在包含json_decode()后,在 window 单击时没有任何反应,调用没有返回任何内容,也没有任何内容记录到控制台。

Without an example, it's hard for me to tell if I'm doing the AJAX cycle wrong, or if there's a different problem somewhere else in my code.如果没有示例,我很难判断我是否在执行 AJAX 循环错误,或者我的代码中的其他地方是否存在不同的问题。 So please, for the love of all that is holy, will someone please post or link me to a self-contained example showing how to make, process, and receive an AJAX call with parameters to a PHP backend?所以,为了所有神圣的事物,请有人将我发布或链接到一个独立的示例,展示如何制作、处理和接收带有 PHP 后端参数的 AJAX 调用? It's incomprehensible that this doesn't already exists somewhere on the internet, but I've been searching and asking for days, and I can't find a single example anywhere.令人难以理解的是,这在互联网上的某个地方还不存在,但我一直在搜索和询问几天,但我在任何地方都找不到一个例子。 This is without jQuery.这没有 jQuery。

Thank you in advance.先感谢您。

Create an mcve and debug this piece by piece.创建一个mcve并逐个调试。

<?php
   $data = json_decode('{ "test": 123 }');
   echo $data;

Will output:请问output:

Recoverable fatal error: Object of class stdClass could not be converted to string in /...../test.php on line 4可恢复的致命错误:class stdClass 的 Object 无法在 /...../test.php 中的第 4 行转换为字符串

You've got an object representing the data decoded from JSON.你有一个 object 代表从 JSON 解码的数据。 You need to echo a useful part of it.您需要回应其中有用的部分。 eg例如

<?php
   $data = json_decode('{ "test": 123 }');
   echo $data->test;

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

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