Ajax的jsonp方式跨域获取数据的简单实例
jsonp的调用,今天碰到了,正好整理了一下。
<!DOCTYPE html>
<html>
<head>
<script src="http://libs.baidu.com/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#b01").click(function(){
$.ajax({//danielinbiti.txt文件内容:getAInfo(["<input type='text' value='1222'/>"])
url: 'http://192.168.12.21:8080/systemr/danielinbiti.txt',
dataType: 'jsonp',//跨域设置jsonp
processData: false,
jsonpCallback:'getAInfo',//与文件中的getAInfo对应
type: 'get',
success: function(data) {
$("#myDiv").html(data);
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
alert('error');
alert(XMLHttpRequest.status);
alert(XMLHttpRequest.readyState);
alert(textStatus);
}
});
});
$("#b02").click(function(){// 需要在后台根据request获取callback,然后才callback(需要返回的json内容)的方式返回,getJSON是ajax的简化,只支持json格式
$.getJSON("http://192.168.12.21:8080/systemr/b.do?jsoncallback=?",function(result){
$("#myDiv").html(result);
});
});
});
</script>
</head>
<body>
<div id="myDiv"><h2>通过 AJAX 改变文本</h2></div>
<button id="b01" type="button">改变内容01</button>
<button id="b02" type="button">改变内容02getJSON</button>
</body>
</html> 相关推荐
yangkang 2020-11-09
lbyd0 2020-11-17
KANSYOUKYOU 2020-11-16
wushengyong 2020-10-28
腾讯soso团队 2020-11-06
Apsaravod 2020-11-05
PeterChangyb 2020-11-05
gyunwh 2020-11-02