zoukankan      html  css  js  c++  java
  • jquery $.getJSON()跨域请求

    1,同一域名下和其他的请求可以是一样的
    var url="http://localhost:2589/a.ashx";
    $(function(){
    $.getJSON(url,function(data){
    alert (data.Name);
    })
    });

    服务器返回字符串:
    {"Name":"loogn","Age":23}
    2,不同域名下
    var url="http://localhost:2589/a.ashx?callback=?";
    $(function(){
    $.getJSON(url,function(data){
    alert (data.Name);
    })
    });

    服务器返回字符串:
    jQuery1706543070425920333_1324445763158({"Name":"loogn","Age":23})
    返回的字符串就是一个调用一个叫“jQuery1706543070425920333_1324445763158” 的函数,参数是{"Name":"loogn","Age":23}。
    其实这个很长的函数名是请求路径中callback=?的作用,我想应该是这样的:$.getJSON方法生成一个对回调方法的引用的名字,换掉?。上面请求会变成
    http://localhost:2589/a.ashx?callback=jQuery1706543070425920333_1324445763158&_=1324445763194,所服务器回返json时要处理一下,如:
    string cb = context.Request["callback"];
    context.Response.Write(cb + "(" + json + ")");

    参数名callback也可换成jsoncallback,我想是怕冲突吧,jsoncallback应该优先检测,没有再检测callback(没测试!!)
    ?也可是具体的函数名,这样回调函数就不能是匿名的了,用?生成只是jQuery为我们的一般操作提供的一个便利。

    个人作品展示:www.jinyuanbao.cn
  • 相关阅读:
    [LeetCode] Search for a Range
    [C++] extern关键字的作用
    [LeetCode] Sentence Similarity
    [LeetCode] Flood Fill
    [LeetCode] Can Place Flowers
    [LeetCode] Intersection of Two Linked Lists
    [LeetCode] My Calendar II
    [LeetCode] My Calendar I
    [LeetCode] Self Dividing Numbers
    [LeetCode] Range Sum Query
  • 原文地址:https://www.cnblogs.com/jyb2014/p/4019188.html
Copyright © 2011-2022 走看看