zoukankan      html  css  js  c++  java
  • AJAX远程跨域获取数据

     1 //本地获取json文件
     2 $.ajax({
     3     url : 'json.php',
     4     type : 'post',
     5     dataType : 'json',//返回json数据格式
     6     success : function(response, status, xhr){
     7         alert(response.a);
     8     }
     9 });
    10 
    11 //dataTyp为jsonp的形式获取远程数据。
    12 $.ajax({
    13     url : 'http://m.xxx.com/json.php',
    14     type : 'post',
    15     dataType : 'jsonp',
    16     success : function(resp){
    17         alert(resp.a);
    18     },
    19     error : function(){
    20         alert('error');
    21     }
    22 });
    23 
    24 //URL加参数获取远程数据
    25 $.ajax({
    26     url : 'http://m.xxx.com/json.php?callback=?',
    27     type : 'post',
    28     dataType : 'json',
    29     success : function(resp){
    30         alert(resp.a);
    31     }
    32 });

    PHP文件:

    1 <?php 
    2     $arr = array('a'=>1, 'b'=>2, 'c'=>3);
    3     //数组转换成json格式
    4     $res = json_encode($arr);    //{"a":1,"b":2,"c":3}
    5     $callback = $_GET['callback'];
    6     echo $callback . "($res)";
    7 ?>
  • 相关阅读:
    javajava.lang.reflect.Array
    基于annotation的spring注入
    jquery插件
    spring的注入方式
    jqueryajax
    javascript基础
    xml基础
    js 获取FCKeditor 值
    TSQL 解析xml
    Linq
  • 原文地址:https://www.cnblogs.com/cloak/p/4985552.html
Copyright © 2011-2022 走看看