Home  >  Article  >  Backend Development  >  php 怎么接受来自 jQuery ajax 的 json 数据?

php 怎么接受来自 jQuery ajax 的 json 数据?

WBOY
WBOYOriginal
2016-06-06 20:07:30844browse

这是前段,后端PHP怎么收?
求大家给我写个实例,只要输出 echo 我的名字是XXX,我的id是XXX就行了..
XXX换成我传的json.

<code>var aj = $.ajax
({
    url:'02.php',
    //data:nameStr+'&'+idStr,
    data:{ id : '1' , name : '二狗' },
    type:'POST',
    dataType:'json',
    cache:false,
    success:aFunction,
    error : function()
    {
        $("div").append("异常!");
    }
});</code>

回复内容:

这是前段,后端PHP怎么收?
求大家给我写个实例,只要输出 echo 我的名字是XXX,我的id是XXX就行了..
XXX换成我传的json.

<code>var aj = $.ajax
({
    url:'02.php',
    //data:nameStr+'&'+idStr,
    data:{ id : '1' , name : '二狗' },
    type:'POST',
    dataType:'json',
    cache:false,
    success:aFunction,
    error : function()
    {
        $("div").append("异常!");
    }
});</code>

<code>$id = $_POST['id'];
$name = $_POST['name'];
echo '我的名字是'.$name.',我的id是'.$id.'就行了..';</code>

和$_POST[键名]一样用

php('input://')输入流

type: 'post' 的时候用 $_POST
type: 'get' 的时候用 $_GET

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn