[英]How to retrieve data from jQuery.Post() in PHP?
I am currently trying to send a string to a to a php script which will eventually return a JSON file. 我当前正在尝试将字符串发送到php脚本,该脚本最终将返回JSON文件。 Here is code i'm using to send the string: 这是我用来发送字符串的代码:
var str = "testString";
$.post("php/getTimes.php", str,
function(data){
console.log(data.name);
console.log(data.time);
}, "json");
In the 'getTimes' php file I am simply trying to receive the 'str' variable I am passing. 在“ getTimes” php文件中,我只是试图接收我传递的“ str”变量。 Any ideas how to do this? 任何想法如何做到这一点? It seems like it should be pretty simple. 看起来应该很简单。
You have to name attributes in POST data
either with serialized string: 您必须使用序列化字符串为POST data
命名属性:
var data = "str=testString";
$.post("php/getTimes.php", data, function(json) {
console.log(json.name);
console.log(json.time);
}, "json");
or with map: 或带地图:
var data = {
str : "testString"
};
$.post("php/getTimes.php", data, function(json) {
console.log(json.name);
console.log(json.time);
}, "json");
To handle this variable in PHP use: 要在PHP中处理此变量,请使用:
$str = $_POST['str'];
In getTimes.php: 在getTimes.php中:
<?php
$var = $_POST['string']; // this fetches your post action
echo 'this is my variable: ' . $var; // this outputs the variable
?>
Also adjust: 同时调整:
$.post("php/getTimes.php", str,
to 至
$.post("php/getTimes.php", { string: str },
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.