JQuery Ajax passes a value to a page and gets the returned array

Source: Internet
Author: User

In this case, we use Ajax to pass values to a PHP page and process the resulting array through the Json_encode () function, and then return to Ajax, which is part of the code that is extracted in the actual case:

PHP page

    Public Function Showchatname () {        $chat = A (' article ', ' Event ');        $res = $chat->selectname (I (' Get.channel '));        echo Json_encode ($res);    }

The output form of array $res after json_encode processing

{"Channel_name": "ABC"}

Template page

    <script src= ". /.. /public/home/js/jquery.min.js "></script> <script type=" Text/javascript ">//Get Cookie value funct Ion GetCookie (name) {var arr,reg=new RegExp ("(^|)" +name+ "= ([^;]            *)(;|$)");            if (Arr=document.cookie.match (reg)) {return unescape (arr[2]);            }else{return null;            }} $ (function () {var channel = GetCookie (' channels ');            var a = Window.location.host;                $.ajax ({type: ' Get ',//' get ' with background communication URL: '/http '/' +a+ '/article/showchatname ',//communicate with this PHP page                DataType: ' json ',//The value returned from PHP interprets data in JSON form: ' channel= ' +channel, Cache:false, Success:function (msg) {//If the call to PHP succeeds, note that MSG is the object returned, this you can customize if (msg.channel_name! = ") {//channel_nam                        E is a key value of the array var b = msg.channel_name;                        $ ("H1"). Text (b); ReTurn false;        }                }            });        }); </script> <body>        <div id= "Headlinecontainer" > &nbsp ;           


Use $.cookie (' channels ') when prompted: Object doesn ' t support property or method ' cookie '! Do not know what is going on, all of them with JS wrote a method to obtain a cookie, and then directly called.

Click here to download the demo version

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.