ajax 跨域请求 地图搜索返回地址代码


$('#addressSearch').click(function(){
var key = $('#keyword').val();
var url = "http://restapi.amap.com/v3/place/text?s=rsv3&key=ad1287cc3a5ee6724ec59f9bb82129ee&offset=6&page=1&city=0531&language=zh_cn&callback=callback&platform=JS&logversion=2.0&sdkversion=1.3&appname=&csid=186B0C8F-2616-4066-90A0-F53A2AB94E65&keywords="+key;
var _html = "";
$.ajax({
type : "post",
async : false,
url :url,
cache : false,
dataType : "jsonp",
jsonp: "callback",
jsonpCallback:"callback",
success: function(data){
$.each(data.pois, function(i, value) {
var location =this.location;
var msg = location.split(",");
_html += '

  • ';
    _html += '';
    _html += '';
    _html += this.name;
    _html += '
    ';
    _html += this.address;
    _html += '
  • ';
    });
    $('#searchResult').html(_html);
    $('#searchResult').css('display','block');
    }
    })
    })

    PS:客户端的jsonp参数是用来通过url传参,传递jsonpCallback参数的参数名,比较拗口,通俗点讲:

    jsonp: "callbackparam"
    jsonpCallback:"jsonpCallback1"

    这两个参数最终会拼接在请求的url后面,变成 http://www.xxx.com/ajax/xxx.ashx?callbackparam=jsonCallback1
    服务端要获取这个参数值:"jsonCallback1" ,拼接在要输出的JSON数据最前面,不然就算请求成功你也只会看到警告:
    Resource interpreted as Script but transferred with MIME type text/plain:
    http://www.cnblogs.com/mahatmasmile/archive/2013/03/29/2989505.html

    跨域请求
    案例2

    php代码
    $data['count'] = 11111;
    $callback = $_GET['jsoncallback'];
    echo $callback.'('.json_encode($data).')';

    声明:祁志飞||小祁的博客|版权所有,违者必究|如未注明,均为原创|本网站采用BY-NC-SA协议进行授权

    转载:转载请注明原文链接 - ajax 跨域请求 地图搜索返回地址代码


    QQ:137867590