如何使用ajax jsonp将数据从一个域获取到另一个域

How Can I get the data from one domain to another domain using ajax jsonp?

本文关键字:一个 获取 另一个 ajax 何使用 jsonp 数据      更新时间:2023-09-26

EX:我的网站url "http://localhost:54887/CustomOrdering.html",但我想从另一个网站"http://localhost:27746/Orders.aspx"获取数据。为此,我在CustomOrdering.html 中写道

function SessionLogin() {                    
                $.ajax({
                    type: "GET",
                    dataType: "jsonp",
                    contentType: "application/json",
                    async: false,
                    url: 'http://localhost:27746/Orders.aspx/SessionLogin',                       
                    success: function (msg) {
                    },
                    error: function (jqXHR, textStatus, errorThrown) {
                        alert(errorThrown);
                    }
                }); 
            }     

Orders.aspx

 public  void SessionLogin()
    {
        HttpContext.Current.Response.ContentType = "application/json";
        string qs = HttpContext.Current.Request.QueryString["callback"];
        HttpContext.Current.Response.Write(qs+ "( [{ '"x'": 10, '"y'": 15}] )");
    }

我正在获取错误jQuery11110002214477863162756_1449484451326 was not called,并在控制台中显示错误消息"Uncaught SyntaxError:意外令牌<"并点击它,指向Orders.aspx设计页面,启动"错误图像"

另一个服务器需要用您给定的回调名称包装返回值:

 public  void SessionLogin()
    {
        HttpContext.Current.Response.ContentType = "application/json";
        string qs = HttpContext.Current.Request.QueryString["callback"];
        HttpContext.Current.Response.Write( qs + "( [{ '"x'": 10, '"y'": 15}] )");
    }