php语言

php跨域调用json的例子

时间:2024-11-02 05:01:40 php语言 我要投稿
  • 相关推荐

php跨域调用json的例子

  JSON和XML没什么太大区别,不过JSON有一个更大范围的应用,那就是,跨域的数据调用。由于安全性问题,AJAX不支持跨域调用,这样要调用不同域名下的数据,很麻烦。下面这个例子,足以展示php用json如何进跨域调用了。

  index.html

  复制代码 代码如下:

  <script type="text/javascript">

  function getProfile(str) {

  var arr = str;

  document.getElementById('nick').innerHTML = arr.nick;

  }

  </script>

  <body><p id="nick"></p></body>

  <script type="text/javascript" src="http://www.openphp.cn/demo/profile.php"></script>

  被调文件profile.php

  复制代码 代码如下:

  <?php

  $arr = array(

  'name' => 'tanteng',

  'nick' => 'pony',

  'contact' => array(

  'email' => 'a@gmail.com',

  'website' => 'http://aa.sinaapp.com',

  )

  );

  $json_string = json_encode($arr);

  echo "getProfile($json_string)";

  ?>

  当index.html调用profile.php时,JSON字符串生成,并作为参数传入getProfile,然后将昵称插入到p中,这样一次跨域数据交互就完成了,是不是特别简单。

【php跨域调用json的例子】相关文章:

PHP处理Ajax请求与Ajax跨域问题05-26

PHP中Json应用09-05

PHP调用的C代码08-05

PHP如何递归实现json类06-27

php调用父类方法08-04

PHP调用C代码的方法11-02

在PHP语言中使用JSON09-14

php调用外部shell的方法总结10-03

PHP编程:类和对象、方法调用09-26

php多图片上传的简单例子10-09