jQuery中用ajax访问php接口文件
js代码
function ajax_request(){ var result; var articleId = new Object(); articleId=getArticleId(); $.ajax({ url: "/topicPage/getComment.php",//请求php文件的路径 data:{id:articleId},//请求中要传送的参数,会自动拼接成一个路径,在php中用get方式获取参数值 dataType: "json",//请求数据类型JSON type: "get",//请求发方式get async:false, success: function(json) { result = json.data;//j将请求到的数据返回给result } }); return result;
php代码
<?php $url="http://api.xxx.com/api/v1/tr/xxxx/1138121/comments?strategy=hot"; $headers=array( 'common-info:{"device_platform": "web","language":"tr","auth_type":xxx,"user_id":'.xxxx.', "version_code": xxxx}' ); $curl=curl_init(); curl_setopt($curl,CURLOPT_URL,$url); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_NOBODY, FALSE); curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); $result=curl_exec($curl); curl_close($curl); echo $result; ?>