Java:
boolean jsonP = false; String cb = request.getParameter("callback"); if (cb !=null) { jsonP true response.setContentType("text/javascript"}else.setContentType("application/x-json"Writerout.getWriter();jsonPout.write+ "(".printdataBlock.toJsonString());");"}
PHP:
$callback = $_REQUEST['callback']; // Create the output object. $output array'a'=> 'Apple','b'=>'Banana' //start output$callback header('Content-Type: text/javascript' echo $callback '(' json_encode$output');'{'Content-Type: application/x-json'); echo json_encode}
ASP.net:
String jsonString "{success: true}" Request.ParamsGetString responseString ""(!String.IsNullOrEmptycb)){ responseString "("")"{= jsonStringResponseWrite(responseString);jsonp客户端代码:
<html>
<head>
<script type='text/javascript'>
function mycallback(record) {
alert(record);//record是返回的数据
}
function jsonPRequest(){
var url = 'http://www.xxx.com/jsonP/request_data.php?callback=mycallback';
var script = document.createElement('script');
script.setAttribute('src',url);
document.getElementsByTagName('head')[0].appendChild(script);
</script>
</head>
<body>
<!-- 点此按钮请求数据 -->
<button onclick='void jsonPRequest();'>请求数据</button>
</body>
</html>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。