leisure
leisure
  • 发布:2014-12-06 17:24
  • 更新:2014-12-07 09:44
  • 阅读:4512

plus.net.XMLHttpRequest() post提交json数据,服务器不能接收到,只能是字符串形式

分类:HBuilder
function setAjax(){  
    if(xhr){  
        xhr.abort();  
        xhr=null;  
    }  

    //setHeader('Content-Type', settings.contentType || 'application/x-www-form-urlencoded');  

    var data = {  
            name:55  
        };  
    xhr=new plus.net.XMLHttpRequest();  
    xhr.onreadystatechange=xhrStatechange;  
    xhr.open( "POST", "http://192.168.1.1:7369/axapp/m/status.do" );  
    xhr.setRequestHeader("Content-type", "application/json; charset=utf-8");  
    alert(JSON.stringify(data));  
    //xhr.send('name=4444');  
    //xhr.send(JSON.stringify(data));  
    xhr.send(data);  
}  

服务端PHP  

<?php  
$name = $_POST['name'];  
$a = array('name'=>$name);  
echo json_encode($a);  
?>
2014-12-06 17:24 负责人:无 分享
已邀请:
Culm

Culm

http://html5plus.org/#specification#/specification/XMLHttpRequest.html
看下教程,send只能传string。
xhr.send(JSON.stringify(data));在服务器端把string转为json格式。

该问题目前已经被锁定, 无法添加新回复