跨域解决方案之二---jsonp
…….续前缘……. <?php header('Content-type: application/json'); // 获取回调函数名 $jsoncallback = htmlspecialchars($_REQUEST ['jsoncallback']); // json数据 $json_data = ["customername_first","customername_second"]; // 输出jsonp格式的数据 echo $jsoncallback."(".json_encode($json_data).")"; ?>
应该很好理解的吧?! <!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<div id="divCustomers"></div>
</body>
<script id="first" type="text/javascript"> function callbackFunction(result,methodName){ console.log(result); var _html = '<ol>'; for (var i = 0; i < result.length; i++) { _html += '<li>' + result[i] + '</li>'; } _html += '</ol>'; document.getElementById('divCustomers').innerHTML = _html; } </script>
<script id="second" type="text/javascript" src="http://localhost/cross_domain/php/jsonp.php?jsoncallback=callbackFunction"></script>
</html>
主要强调下匹配关系: (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |