Home > Backend Development > PHP Tutorial > ajax - xmlhttprequest send json在php端接受数据的问题

ajax - xmlhttprequest send json在php端接受数据的问题

WBOY
Release: 2016-06-06 20:45:03
Original
1242 people have browsed it

<code class="lang-javascript">    var uinfo   = {};
    var uname   = document.getElementById('uname'); 
    var upwd    = document.getElementById('upwd');  
    uinfo['uname']  = uname.value;
    uinfo['upwd']   = upwd.value; 
    var usent = JSON.stringify(uinfo);
    var xhr = null;
    if(window.XMLHttpRequest){
        xhr = new XMLHttpRequest();
    }else{
        xhr = ActiveXObject('Microsoft.XMLHttp');
    }
    xhr.open("POST", "/controler/login.php",true);
    //etc
     xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    xhr.send(usent);
</code>
Copy after login
Copy after login

然后php端怎么接受js发送的数据呢?
$_POST['']
拿不到数据,这部分的格式具体是要指定呢,求大神指导下~~

多谢~!

回复内容:

<code class="lang-javascript">    var uinfo   = {};
    var uname   = document.getElementById('uname'); 
    var upwd    = document.getElementById('upwd');  
    uinfo['uname']  = uname.value;
    uinfo['upwd']   = upwd.value; 
    var usent = JSON.stringify(uinfo);
    var xhr = null;
    if(window.XMLHttpRequest){
        xhr = new XMLHttpRequest();
    }else{
        xhr = ActiveXObject('Microsoft.XMLHttp');
    }
    xhr.open("POST", "/controler/login.php",true);
    //etc
     xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    xhr.send(usent);
</code>
Copy after login
Copy after login

然后php端怎么接受js发送的数据呢?
$_POST['']
拿不到数据,这部分的格式具体是要指定呢,求大神指导下~~

多谢~!

答案太弱了...

<code>xhr.send('json='+usent);
</code>
Copy after login

你可以用file_get_contents(‘php://input’);尝试打印看看是什么,可以去看看这一篇文章

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template