찾다
php教程php手册PHP 与 js json的通信实例

简介一下json吧,json(object notation) 是一种轻量级的数据交换格式,易于人阅读和编写,同时也易于机器解析和生成,它基于javascript programming language,standard ecma-262 3rd edition - december 1999的一个子集,json采用完全独立于语言的文本格式,但是也使用了类似于c语言家族的习惯(包括c, c++, c#, java, javascript, perl,python等),这些特性使json成为理想的数据交换语言.

json建构于两种结构:

"名称/值"对的集合(a collection of name/value pairs),不同的语言中,它被理解为对象(object),纪录(record),结构(struct),字典(dictionary),哈希表(hash table),有键列表(keyed list),或者关联数组(associative array).

值的有序列表(an ordered list of values),在大部分语言中,它被理解为数组(array).

php文件代码:

<?php  
    $res[&#39;id&#39;] = $_post[&#39;id&#39;];  
    $res[&#39;name&#39;] = "elar";  
    $res[&#39;age&#39;] = "21";  
    $response = "hello this is response".$_post[&#39;id&#39;];  
    echo json_encode($res);  
?>

js代码:

<script type="text/javascript">  
    function getjson() {
        var xmlhttp;
        try {
            // firefox, opera 8.0+, safari
            xmlhttp = new xmlhttprequest();
        } catch (e) {
            // internet explorer
            try {
                xmlhttp = new activexobject("msxml2.xmlhttp");
            } catch (e) {
                try {
                    xmlhttp = new activexobject("microsoft.xmlhttp");
                } catch (e) {
                    alert("您的浏览器不支持ajax!");
                    return false;
                }
            }
        }
        xmlhttp.onreadystatechange = function () {
            if (xmlhttp.readystate == 4) {
                //alert(xmlhttp.responsetext);
                var str = xmlhttp.responsetext;
                document.getelementbyid(&#39;show&#39;).innerhtml += str;
                //alert(str);
                var obj = eval(&#39;(&#39; + xmlhttp.responsetext + &#39;)&#39;);
                //var obj = eval(({"id":"123","name":"elar","age":"21"}));
                alert(obj.name);
            }
        }
        var data = "id=123";
        xmlhttp.open("post", "testjson.php", true);
        xmlhttp.setrequestheader("content-type", "application/x-www-form-urlencoded");
        xmlhttp.send("id=123");
    }
</script>

 


 

 


本文地址:

转载随意,但请附上文章地址:-)

성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.

핫 AI 도구

Undresser.AI Undress

Undresser.AI Undress

사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover

AI Clothes Remover

사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool

Undress AI Tool

무료로 이미지를 벗다

Clothoff.io

Clothoff.io

AI 옷 제거제

AI Hentai Generator

AI Hentai Generator

AI Hentai를 무료로 생성하십시오.

인기 기사

R.E.P.O. 에너지 결정과 그들이하는 일 (노란색 크리스탈)
3 몇 주 전By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. 최고의 그래픽 설정
3 몇 주 전By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. 아무도들을 수없는 경우 오디오를 수정하는 방법
3 몇 주 전By尊渡假赌尊渡假赌尊渡假赌
WWE 2K25 : Myrise에서 모든 것을 잠금 해제하는 방법
3 몇 주 전By尊渡假赌尊渡假赌尊渡假赌

뜨거운 도구

Atom Editor Mac 버전 다운로드

Atom Editor Mac 버전 다운로드

가장 인기 있는 오픈 소스 편집기

Eclipse용 SAP NetWeaver 서버 어댑터

Eclipse용 SAP NetWeaver 서버 어댑터

Eclipse를 SAP NetWeaver 애플리케이션 서버와 통합합니다.

스튜디오 13.0.1 보내기

스튜디오 13.0.1 보내기

강력한 PHP 통합 개발 환경

VSCode Windows 64비트 다운로드

VSCode Windows 64비트 다운로드

Microsoft에서 출시한 강력한 무료 IDE 편집기

ZendStudio 13.5.1 맥

ZendStudio 13.5.1 맥

강력한 PHP 통합 개발 환경