zoukankan      html  css  js  c++  java
  • jsonp跨域

    js调用问题

    最近用js调用另一个站点时报错了,报错信息:No 'Access-Control-Allow-Origin' header is present on the requested resource。js跨域问题。

    后台C#接口

    使用默认的回调函数:

        public class DemoController : ApiController
        {
            [Route("~/demo")]
            [HttpGet]
            public HttpResponseMessage Demo(int OrderId) //C#传参数不区分大小写
            {
                HttpResponseMessage response = Request.CreateResponse();
                response.StatusCode = HttpStatusCode.OK;
                response.Content = new StringContent(String.Format("callbackFun({0})", (new { IsSuccess = "-1000", Msg = "resp.Msg" }).ToJson()));
                return response;
            }
        }

    前台js调用示例:(回调函数和后台指定一致,可以不申明function callbackFun(data)函数,默认success函数会被调用;如果申明了callbackFun函数,会先调callbackFun,然后再调success函数)

    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>jquery</title>
    <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
    <script type="text/javascript">
    function b_click(){
        $.ajax({
            type:"GET",
            async:false,
            dataType: 'jsonp', //跨域调用
          jsonpCallback: "callbackFun",  //指定回调函数名称
            data: {"orderId":4811},
            //timeout: 5000, //超时设置
            //contentType: "application/json;utf-8",
            url:"http://ab.test.com/demo",
            success:function(result){
                console.log("success");
                console.log(result);
                alert(result.Msg);
            },
            error:function(result){
                console.log("fail");
                console.log(result);
                alert(result.Msg);
            }
        });
        
        //function callbackFun(data){
            //console.log("call");
        //}
    }
    </script>
    </head>
    <body data-spy="scroll" data-target=".navbar-example">
     <input type="button" value="click me!" onclick="b_click();" />
    </body>
    </html>

     浏览器看js请求链接:http://ab.test.com/demo?callback=callbackFun&orderId=4811

    可以看出:如果不指定回调参数名称,默认是callback

    指定回调函数名:

    服务端接口:

        public class DemoController : ApiController
        {
            [Route("~/demo")]
            [HttpGet]
            public HttpResponseMessage DemoCheck(int OrderId, String callbackMethod)
            {
                HttpResponseMessage response = Request.CreateResponse();
                response.StatusCode = HttpStatusCode.OK;
                response.Content = new StringContent(String.Format(callbackMethod + "({0})", (new { IsSuccess = "-1000", Msg = "resp.Msg" }).ToJson()));
                return response;
            }
        }

    接口返回结果:

    指定回调参数名和值:

    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>jquery</title>
    <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
    <script type="text/javascript">
    function b_click(){
        $.ajax({
            type:"POST",
            async:false,
            dataType: 'jsonp',
         //jsonp的值自定义,如果使用callbackMethod,那么服务器端,要返回一个callbackMethod的值对应的对象. jsonp:
    'callbackMethod', jsonpCallback: "callbackFun", //指定回调函数名称 data: {"orderId":4811}, //timeout: 5000, //contentType: "application/json;utf-8", url:"http://ab.test.com/demo", success:function(result){ console.log("success"); console.log(result); alert(result.Msg); }, error:function(result){ console.log("fail"); console.log(result); alert(result.Msg); } }); } </script> </head> <body data-spy="scroll" data-target=".navbar-example"> <input type="button" value="click me!" onclick="b_click();" /> </body> </html>

     请求url:http://ab.test.com/demo?callbackMethod=callbackFun&orderId=4811

  • 相关阅读:
    多线程之旅:避免死锁——简单的锁分级(锁排序)
    和我一起来学iOS(三)UIView及其子类(上)
    谈谈.NET中常见的内存泄露问题——GC、委托事件和弱引用
    和我一起来学iOS(二)iOS中的一些约定、模式与三种回调机制
    多线程之旅六——异步编程模式,自己实现IAsyncResult
    详解JavaScript中的函数与闭包
    和我一起来学iOS(一)ObjectC的语法
    浅谈SQL SERVER中的物理联接算法
    多线程之旅之三——Windows内核对象同步机制
    深入 聚集索引与非聚集索引(一)
  • 原文地址:https://www.cnblogs.com/mr-yang-localhost/p/7684653.html
Copyright © 2011-2022 走看看