Jquery ajax jsonp calls instance code across domains

  • 2020-03-30 00:49:28
  • OfStack

Client code


<%@ 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>

Server-side code


 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();
        }
    }

The client page and the server page are in two projects for cross-domain invocation testing.

Cross-domain instance code (need to load jquery, the page is utf-8) :


 <!-- Pull out recruitment data -->
  <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"> More and more </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>

The json code:


success_jsonpCallback([{"city":" Guangzhou ","companyName":"POCO.CN","createTime":"15:02 release ","posiitonDetailUrl":"http://www.lagou.com:80/jobs/16868.html","positionAdvantage":" In the cohesion of the team, the old city to work convenient transportation, double break ","positionName":" Commercial front-end development engineer ","salary":"4k-7k"},{"city":" Beijing ","companyName":" Meitong yundong (Beijing) technology co., LTD ","createTime":"14:47 release ","posiitonDetailUrl":"http://www.lagou.com:80/jobs/16866.html","positionAdvantage":"Html5 The best technical team ","positionName":"Web The front-end development ","salary":"4k-8k"},{"city":" hangzhou ","companyName":" Pocket shopping ","createTime":"14:42 release ","posiitonDetailUrl":"http://www.lagou.com:80/jobs/13024.html","positionAdvantage":" Broad development platform, where self-worth is reflected ","positionName":"web Front-end development engineer ","salary":"8k-12k"},{"city":" Beijing ","companyName":" The pudding mobile ","createTime":"14:02 release ","posiitonDetailUrl":"http://www.lagou.com:80/jobs/1498.html","positionAdvantage":" Three meals, around the beauty such as clouds ","positionName":"Android Development engineer ","salary":"10k-20k"},{"city":" Beijing ","companyName":" The pudding mobile ","createTime":"14:02 release ","posiitonDetailUrl":"http://www.lagou.com:80/jobs/2539.html","positionAdvantage":" Three meals, small bridge water family, beauty ","positionName":"ios Development engineer ","salary":"10k-20k"},{"city":" Shanghai ","companyName":" ttpod ","createTime":"00:55 release ","posiitonDetailUrl":"http://www.lagou.com:80/jobs/11494.html","positionAdvantage":" Entrepreneurial atmosphere   Small and beautiful ","positionName":"Android Development engineer ","salary":"8k-16k"},{"city":" Beijing ","companyName":"LBE Master safety ","createTime":"11:39 release ","posiitonDetailUrl":"http://www.lagou.com:80/jobs/5983.html","positionAdvantage":" Five social insurance and one housing fund   Performance bonus ","positionName":"Android Development engineer ","salary":"8k The above "},{"city":" Beijing ","companyName":" Dim sum mobile ","createTime":"11:24 release ","posiitonDetailUrl":"http://www.lagou.com:80/jobs/16736.html","positionAdvantage":" Technology-oriented team atmosphere, comprehensive benefits ","positionName":"Android","salary":"15k-25k"},{"city":" Guangzhou ","companyName":" litchi FM","createTime":"10:44 release ","posiitonDetailUrl":"http://www.lagou.com:80/jobs/16634.html","positionAdvantage":" Even running and keeping the weight off are rewarded! ","positionName":"WP Mobile phone development engineer ","salary":"16k-25k"},{"city":" Beijing ","companyName":" Net silver - Jd subsidiary ","createTime":"10:08 release ","posiitonDetailUrl":"http://www.lagou.com:80/jobs/14162.html","positionAdvantage":" In charge of jingdong mall - Internet financial products  JS The development of ","positionName":"Javascript  Front-end development engineer ","salary":"10k-20k"}])


Related articles: