如何从PHP中的jQuery.Post()中检索数据? [英] How to retrieve data from jQuery.Post() in PHP?

查看:94
本文介绍了如何从PHP中的jQuery.Post()中检索数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试将字符串发送到一个php脚本,该脚本最终会返回一个JSON文件。
这是我用来发送字符串的代码:

I am currently trying to send a string to a to a php script which will eventually return a JSON file. 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");

在'getTimes'php文件中我只是试图接收我传递的'str'变量。任何想法如何做到这一点?看起来应该很简单。

In the 'getTimes' php file I am simply trying to receive the 'str' variable I am passing. Any ideas how to do this? It seems like it should be pretty simple.

推荐答案

您必须在 POST data 使用序列化字符串:

You have to name attributes in POST data either with serialized string:

var data = "str=testString";
$.post("php/getTimes.php", data, function(json) {
    console.log(json.name);
    console.log(json.time);
}, "json");

或使用地图:

var data = {
    str : "testString"
};

$.post("php/getTimes.php", data, function(json) {
    console.log(json.name);
    console.log(json.time);
}, "json");

要在PHP中处理此变量,请使用:

To handle this variable in PHP use:

$str = $_POST['str'];

这篇关于如何从PHP中的jQuery.Post()中检索数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆