加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

JSONP实现代码

发布时间:2020-12-16 19:09:29 所属栏目:百科 来源:网络整理
导读:Java: boolean jsonP = false ; String cb = request . getParameter ( "callback" ); if ( cb != null ) { jsonP true response . setContentType ( "text/javascript" } else . setContentType ( "application/x-json" Writer out . getWriter (); jsonP o

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>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读