萬盛學電腦網

 萬盛學電腦網 >> 腳本專題 >> javascript >> jquery ajax jsonp跨域調用實例代碼

jquery ajax jsonp跨域調用實例代碼

 今天研究了AJAX使用JSONP進行跨域調用的方法,發現使用GET方式和POST方式都可以進行跨域調用,這裡簡單分享下,方便需要的朋友

客戶端代碼     代碼如下: <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApp.WebForm1" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <script src="jquery-1.7.1.min.js" type="text/javascript"></script> <script type="text/javascript">     function aa() {         $.ajax({             url: "http://localhost:12079/WebForm2.aspx",             data: "p1=1&p2=2&callback=?",             type: "post",             processData: false,             timeout: 15000,             dataType: "jsonp",  // not "json" we'll parse             jsonp: "jsonpcallback",             success: function(result) {             alert(result.value1);             }         });     }   </script>     <title></title> </head> <body>     <form id="form1" runat="server">     <div>       </div>     </form>     <p>         <input id="Button1" type="button" value="button" onclick="aa()" /></p> </body> </html>     服務器端代碼     代碼如下:  public partial class WebForm2 : System.Web.UI.Page     {         protected void Page_Load(object sender, EventArgs e)         {                        string callback = Request["callback"];              string v1="1";             string v2="2";             string response = "{"value1":"" + v1 + "","value2":"" + v2 + ""}";             string call = callback + "(" + response + ")";             Response.Write(call);             Response.End();         }     }     客戶端頁面和服務器端頁面在兩個項目中,以便進行跨域調用測試。   跨域實例代碼(需要加載jquery,頁面為utf-8編碼):     代碼如下:  <!--拉勾招聘數據-->   <script type="text/javascript">    function success_jsonpCallback(data){     var html = '';     var pos = '';     html += '<ul>';     jQuery.each(data, function(k, v) {                  if(k<10){                   pos = '【' + v.city+ '】' + v.positionName + '('+ v.salary +') - '+v.companyName;       if(pos.length > 20){        pos = pos.substring(0,19)+'...';                      }                      html += '<li><a href="'+v.posiitonDetailUrl+'" target="_blank" title="【' + v.city+ '】' + v.positionName + '('+ v.salary +') - '+v.companyName+'">'+pos+'</a></li>';                  }     });     html += '</ul><div class="more-link"><a href="http://www.lagou.com/jobs/list_%E5%89%8D%E7%AB%AF%E5%BC%80%E5%8F%91" target="_blank">更多</a></div>';     jQuery('#lagouData').html(html);    }      function getLagouData() {     jQuery.ajax({      async:false,      url: "http://www.lagou.com/join/listW3cplus?kd=%E5%89%8D%E7%AB%AF%E5%BC%80%E5%8F%91",      type: "GET",      dataType: "jsonp",      jsonpCallback: 'success_jsonpCallback',      contentType: "application/jsonp; charset=utf-8",      success: function(data) {       success_jsonpCallback(data);      }     });    }    getLagouData();         </script>       <div id="lagouData"></div>     jsonp代碼:     復制代碼 代碼如下: success_jsonpCallback([{"city":"廣州","companyName":"POCO.CN","createTime":"15:02發布","posiitonDetailUrl":"http://www.lagou.com:80/jobs/16868.html","positionAdvantage":"身處凝聚力團隊,老城區上班交通便利,雙休","positionName":"商業前端開發工程師","salary":"4k-7k"},{"city":"北京","companyName":"美通雲動(北京)科技有限公司","createTime":"14:47發布","posiitonDetailUrl":"http://www.lagou.com:80/jobs/16866.html","positionAdvantage":"Html5技術最棒的團隊","positionName":"Web前端開發","salary":"4k-8k"},{"city":"杭州","companyName":"口袋購物","createTime":"14:42發布","posiitonDetailUrl":"http://www.lagou.com:80/jobs/13024.html","positionAdvantage":"廣闊的發展平台、自我價值體現的地方","positionName":"web前端開發工程師","salary":"8k-12k"},{"city":"北京","companyName":"布丁移動","createTime":"14:02發布","posiitonDetailUrl":"http://www.lagou.com:80/jobs/1498.html","positionAdvantage":"三餐、周圍美女如雲","positionName":"Android開發工程師","salary":"10k-20k"},{"city":"北京","companyName":"布丁移動","createTime":"14:02發布","posiitonDetailUrl":"http://www.lagou.com:80/jobs/2539.html","positionAdvantage":"三餐,小橋流水人家,美女","positionName":"ios開發工程師","salary":"10k-20k"},{"city":"上海","companyName":"天天動聽","createTime":"00:55發布","posiitonDetailUrl":"http://www.lagou.com:80/jobs/11494.html","positionAdvantage":"創業氛圍 講求小而美","positionName":"Android開發工程師","salary":"8k-16k"},{"city":"北京","companyName":"LBE安全大師","createTime":"11:39發布","posiitonDetailUrl":"http://www.lagou.com:80/jobs/5983.html","positionAdvantage":"五險一金 績效獎金","positionName":"Android開發工程師","salary":"
copyright © 萬盛學電腦網 all rights reserved