Ajax requests multiple picture data

Source: Internet
Author: User

Requirements Analysis:

Enables the display of images on a Web page. The picture content has changed because the read picture name is the same. Use of the Internet requires that the browser does not cache the operation does not take effect

<METAhttp-equiv= "Pragma"CONTENT= "No-cache">     <METAhttp-equiv= "Cache-control"CONTENT= "No-cache, must-revalidate">     <METAhttp-equiv= "Expires"CONTENT= "0">    <MetaCharSet= "Utf-8">    <METAhttp-equiv= "Expires"CONTENT= "Wed, 1997 08:21:57 GMT">     <METAhttp-equiv= "Expires"CONTENT= "0"> 

Later, using AJAX to request data, directly from the back-end to read the picture content and return to the front end, so as to achieve the latest picture data display

Front-End AJAX requests:

$.ajax ({url: ' index.php ', type: ' POST ', DataType: ' JSON ', data: { Mode: '<?= $Mode?>', ID: '<?= $idVal;?>'} '). Success (function (data) {//Console.log (data);                $.each (Data,function (name,value) {//Console.log (name);                Console.log (value);                            $ ("#sm_image" +name). attr ("src", "data:image/png;base64," +value);            }). Done (function (data) {Console.log ("success");            $ ("Sm_image"). attr ("src", "data:image/png;base64," +data);            echo}). Fail (function (data) {Console.log (data);        Console.log ("error");        }). Always (function () {Console.log ("complete");        }); 

Background PHP parsing and return:

<?PHPif($_post) {   Switch($_post[' Mode ']) {        Case1://Print_r ($_post[' id ');            $IDARR=Explode(",",$_post[' ID ']); $jsonStr= "{";  for($i= 0;$i<Count($IDARR) ;$i++) {                $img=Base64_encode(file_get_contents("/web/image/image_id".$IDARR[$i].". png)); $jsonStr.= "\"".$IDARR[$i]." \":\"$img\","; }            $jsonStr=substr($jsonStr, 0,-1); $jsonStr.= "}"; Echo $jsonStr;  Break;  Case2:$id=$_post[' ID ']; $img=Base64_encode(file_get_contents("/web/image/image_id".$id.". png)); Echo‘{"‘.$id.‘":"‘.$img.‘"}‘;//read image data and encode base64            Break; default: Break; } }?>

Ajax requests multiple picture data

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.