使用jquery ajax技术传递json数据 php做为服务端

    function responseAdvice(msgid,userid){
        var textid="rep_text_"+msgid;
        var statusid="status_"+msgid;
        var content=document.getElementById(textid).value;
       
        if(content==""){
            alert("不能为空");
            return;
        }
        var timestamp = Date.parse(new Date());
        
        var encoded={
            "userid":userid,
            "content":content,
            "time":timestamp,
            "message_id":msgid
        }
        var jsonstr=JSON.stringify(encoded);
        $.ajax({    
            type:'post',        
            url:'__URL__/response_advice',    
            data:
                {
                    "jsondata":jsonstr
                },      
            dataType:'json',    
            success:function(json){
                document.getElementById(statusid).innerHTML=1;
            }    
        });    

    }

在上面的代码中$.ajax的各个域的类型名是固定的,必须为“type”"data"“url”"dataType" "success"php

其中data中的大括号的方式在php端就能够直接$_POST['jsondata']接收到数据。encoded 还不是json类型,真是小白啊,我刚知道这实际上是js的对象类型,要用JSON.stringify进行转换为json字符串。success中的函数里面的参数就是服务端返回的字符串。
ajax

相关文章
相关标签/搜索