http://www.cnblogs.com/cwp-bg/p/7668840.html ajax和jsonp使用总结 2017-10-17
var requestUrl="http://localhost/1.html?callback=?"; $.ajax({ type : "get", async:true, url : requestUrl, data : "channelid=7¤t=1&rowSize=4", dataType : "jsonp", success :function(data){ console.log("--------(xxxxx)-----success"); }, error:function(data){ console.log("error: (xxxxx)"); } });
注意:跨域要用jsonp,并且双方要有约定callback
php代码:
$callback=$_GET['callback']; echo $callback."($res)";
检测手机app嵌入的H5页面,是否处于联网状态 2016-6-29
var requestUrl="http://localhost/1.html?callback=?"; var ajaxTimeoutTest = $.ajax({ type : "get", timeout: 1000,//超出时间设置,单位毫秒 async:true, url : requestUrl, data : "channelid=7¤t=1&rowSize=4", dataType : "jsonp", success :function(data){ console.log("--------(xxxxx)-----success"); }, error:function(data){ console.log("error: (xxxxx)"); }, complete:function(XMLHttpRequest,status){//请求完成后最终执行参数 if(status == 'timeout' ){ //超时,status还有success,error等值的情况 ajaxTimeoutTest.abort(); alert('超时'); } } });
一个简单的ajax例子
<script src="http://code.jquery.com/jquery-1.11.1.min.js"></script> <script type="text/javascript"> $(function(){ var TEST = { test:function() { //var requestUrl=GLOBAL.WEBSITE+"client/newspaper/getNewspaper.html?callback=?"; var requestUrl="http://121.199.44.182:8090/client/newspaper/getChannel.html?callback=?"; $.ajax({ type : "post", async:true, url : requestUrl, data : "typeid=2&channelid=7", dataType : "jsonp", success :function(data){ console.log("-------------success"); console.log(data); for(var i=0;i<data.length;i++){ console.log(data[i].id); console.log(data[i].name); } }, error:function(data){ console.log("error:"+data[0]); } }); }, }; TEST.test(); }); </script>
用对象方式传参数:
var requestUrl="http://192.168.1.1/expai_data/counter?"; $.ajax({ type : "post", async:true, url : requestUrl, data : "product="+product+"&name="+consignee+"&address="+address+"&phone="+phone+"&content="+content+"&step="+step+"&cpmc="+cpmc+"&src="+src, /* data:{ product : product, name : consignee, address : address, phone : phone, content : content, step : step, cpmc : cpmc, },*/ dataType : "jsonp", success :function(data){ //console.log("--------(xxxxx)-----success"); }, error:function(data){ //console.log("error: (xxxxx)"); } });
一个简单的 $.getJSON 例子:2016-1-18
var targetUrl = "http://192.168.1.34:85/jiazhuang/ajax/?action=getMapInfo&callback=?"; $.getJSON(targetUrl,{ format: "json" }, function(data) { alert(data.type); } );
一个简单的 $.ajax 例子:2016-1-18
var targetUrl = "http://192.168.1.34:85/jiazhuang/ajax/?action=getMapInfo&callback=?"; $.ajax({ type: "get", url: targetUrl, data: {}, dataType: "jsonp", jsonp : "callback", async:true, cache: false, success: function(data) { if(data.type==1){ console.log(data.msg); }else{ console.log(data.msg); } }, error: function(data) { console.log("操作失败"); }, });
json解析成字符串、字符串解析为json:
var book={a:1,b:2}//这是原始的json var jsonBook=JSON.stringify(book);//json解析成字符串 var objectBook=JSON.parse(jsonBook); //字符串解析为json alert(book); alert(jsonBook); alert(objectBook.a);
.........
var requestUrl="http://localhost/1.html?callback=?";var ajaxTimeoutTest = $.ajax({ type : "get", timeout: 1000,//超出时间设置,单位毫秒 async:true, url : requestUrl, data : "channelid=7¤t=1&rowSize=4", dataType : "jsonp", success :function(data){ console.log("--------(xxxxx)-----success"); }, error:function(data){ console.log("error: (xxxxx)"); }, complete:function(XMLHttpRequest,status){//请求完成后最终执行参数 if(status == 'timeout' ){//超时,status还有success,error等值的情况 ajaxTimeoutTest.abort(); alert('超时'); } }});