Blogger Information
Blog 56
fans 1
comment 0
visits 62168
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
JSONP案例ID获取商品信息
零龙
Original
955 people have browsed it

JSONP通过ID货物商品信息

- 准备环境:两个端口不一样,构成跨域请求的条件。

- 获取数据的网址:http://www.a.com 本机地址:http://www.php.com

- goods.html 通过点击事件查看商品信息(本地文件)

- goods.php 处理商品信息,返回

演示代码:goods.html

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>点击按钮获取商品信息</title>
  7. </head>
  8. <body>
  9. <button>查看</button>
  10. <script>
  11. function handle(data)
  12. {
  13. console.log(data);
  14. var ul = document.createElement("ul");
  15. var obj = JSON.parse(data);
  16. ul.innerHTML += '<li>' + obj.title + '</li>';
  17. ul.innerHTML += '<li>' + obj.goods.type + '</li>';
  18. ul.innerHTML += '<li>' + obj.goods.goodname + '</li>';
  19. ul.innerHTML += '<li>' + obj.goods.price + '</li>';
  20. document.body.appendChild(ul);
  21. }
  22. var but =document.querySelector("button");
  23. but.addEventListener("click",function()
  24. {
  25. var script = document.createElement("script");
  26. script.src = "http://www.a.com/goods.php?id=2&jsonp=handle";
  27. document.head.appendChild(script);
  28. });
  29. </script>
  30. </body>
  31. </html>

演示代码:goods.php

  1. <?php
  2. header("Content-type:text/html;charset=utf-8");
  3. //设置默认字符集
  4. $id = $_GET['id'];
  5. $callback = $_GET['jsonp'];
  6. $goodss = [
  7. '{"type":"大家电","goodsname":"电视机","price":"3200"}',
  8. '{"type":"通讯设备","goodsname":"小米10 pro","price":"6800"}',
  9. '{"type":"通讯设备","goodsname":"苹果12 pro","price":"13200"}',
  10. '{"type":"办公设备","goodsname":"联想Thinkpad","price":"6200"}',
  11. '{"type":"家居","goodsname":"罗莱家纺","price":"530"}'
  12. ];
  13. //由于传入ID的值从1开始,通过$id-1才能得到查询的正确id
  14. if(array_key_exists(($id-1),$goodss))
  15. {
  16. //查询$goods中键值等于$id的商品。
  17. $goods = $goodss[$id-1];
  18. }
  19. // $json = '{}'; 定义josn格式
  20. $json = '{
  21. "title":"商品信息",
  22. "goods": '. $goods .'
  23. }';
  24. //返回handle函数
  25. $jsons = json_encode($json);
  26. echo $callback.'('. $jsons .')';

示例图:


事件代理的案例

示例:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>事件代理</title>
  7. </head>
  8. <ul>
  9. <li>1111</li>
  10. <li>2222</li>
  11. <li>3333</li>
  12. <li>4444</li>
  13. <li>5555</li>
  14. </ul>
  15. <body>
  16. <script>
  17. var ul = document.querySelector("ul");
  18. ul.addEventListener("click",function(ev)
  19. {
  20. var lis = ev.target;
  21. if(lis.tagName.toLowerCase()=='li')
  22. {
  23. lis.style.background="red";
  24. }
  25. });
  26. ul.addEventListener("mouseout",function(ev)
  27. {
  28. ev.target.style.background = "";
  29. });
  30. </script>
  31. </body>
  32. </html>

示例图:

Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post