[英]How to capture json data on php that is sent with ajax (no jquery)
I am sending a json data to the server using ajax and pure javascript. 我正在使用ajax和纯javascript将json数据发送到服务器。 How do I get the $_POST
index that will display the json content on the php page? 如何获取将在php页面上显示json内容的$_POST
索引?
ajax send a request as key=value
to the server, meanwhile, by using the content-type 'application/json', I got an example on the link below as the json data (stringfy) was sent directly with no key=value
. ajax将请求作为key=value
发送到服务器,同时,通过使用内容类型'application / json',我在下面的链接上得到了一个示例,因为json数据(stringfy)是直接发送而没有key=value
。
Sending a JSON to server and retrieving a JSON in return, without JQuery 在不使用JQuery的情况下,将JSON发送到服务器并获取JSON作为回报
On the php side for post request, the example below was given. 在php方面,用于发布请求,下面给出了示例。
$v = json_decode(stripslashes(file_get_contents("php://input")))
Now i don't understand what php://input
signifies here since the json data was sent to the same page. 现在我不明白php://input
在这里表示什么,因为json数据已发送到同一页面。 I tried file_get_contents('https://'.$_SERVER['SERVER_NAME'].$_SERVER['PHP_SELF']);
我尝试了file_get_contents('https://'.$_SERVER['SERVER_NAME'].$_SERVER['PHP_SELF']);
but it returned nothing. 但它什么也没返回。 I tried using var_dump($_POST)
to view all the content as an array, but all i get is array(0){}
. 我尝试使用var_dump($_POST)
将所有内容查看为数组,但是我得到的只是array(0){}
。 So how do i actually catch the ajax (json) request i sent to a php page? 那么,我如何真正捕获发送到php页面的ajax(json)请求? Here is an example of the code: 这是代码示例:
var data = {
name : "john",
Friend : "Jonny",
Bestie : "johnson",
neighbour: "john doe"
};
json = JSON.stringify(data);
var ajax = new XMLHttpRequest(), url = '../handler.php';
ajax.onreadystatechange = function() {
if(this.readyState == 4 && this.status == 200) {
console.log(this.responseText);
};
};
ajax.open('POST', url, true);
ajax.setRequestHeader('content-type', 'application/json');
ajax.send(json);
PHP PHP
header("Content-Type: application/json");
var_dump($_POST);
file_get_contents('https://'.$_SERVER['SERVER_NAME'].$_SERVER['PHP_SELF']);
I expected the json string to be present in the $_POST variable and accessible by it's index after decoding the json string, but i get array(0){}
, null
or nothing displayed at all 我希望json字符串存在于$ _POST变量中,并且在解码json字符串后可以通过其索引访问,但是我得到array(0){}
,为null
或根本不显示任何内容
To get an array, add the true
argument to json_decode()
. 要获取数组,请将true
参数添加到json_decode()
。 In your code: 在您的代码中:
$body = json_decode(file_get_contents("php://input"), true);
var_export($body);
To add the JSON data in $_POST, you can use this code. 要在$ _POST中添加JSON数据,可以使用此代码。
In JS: 在JS中:
json = JSON.stringify(data);
var ajax = new XMLHttpRequest(), url = '../handler.php';
ajax.onreadystatechange = function() {
if(this.readyState == 4 && this.status == 200) {
console.log(this.responseText);
};
};
ajax.open('POST', url, true);
ajax.setRequestHeader('content-type', 'application/x-www-form-urlencoded');
ajax.send('jsn='+json);
In PHP: 在PHP中:
$arr = isset($_POST['jsn']) ? json_decode($_POST['jsn'], true) :[];
var_export($arr);
Source: https://coursesweb.net/ajax/json-from-javascript-php 来源: https : //coursesweb.net/ajax/json-from-javascript-php
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.