Home  >  Article  >  Web Front-end  >  php跨域调用json的例子_jquery

php跨域调用json的例子_jquery

WBOY
WBOYOriginal
2016-05-16 17:15:411239browse

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

index.html

复制代码 代码如下:





被调文件profile.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,然后将昵称插入到div中,这样一次跨域数据交互就完成了,是不是特别简单。
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn