码迷,mamicode.com
首页 > Web开发 > 详细

ajax实现jsonp跨域接口

时间:2016-06-30 11:00:02      阅读:169      评论:0      收藏:0      [点我收藏+]

标签:

HTML页面代码:

<script type="text/javascript">

function UrlSearch(){
var name,value;
var str=location.href; //取得整个地址栏
var num=str.indexOf("?")
str=str.substr(num+1); //取得所有参数 stringvar.substr(start [, length ]
var arr=str.split("&"); //各个参数放到数组里
for(var i=0;i < arr.length;i++){
num=arr[i].indexOf("=");
if(num>0){
name=arr[i].substring(0,num);
value=arr[i].substr(num+1);
this[name]=value;
}
}
}

function jsonp(){

var Request=new UrlSearch(); //实例化

var goods_name = Request.name

$.ajax({
type: "GET",
async: false,
url:"http://test.com/jp4.php",
dataType: "jsonp",
data:{goods_name:goods_name},
jsonp: "callback",//传递给请求处理程序或页面的,用以获得jsonp回调函数名的参数名(callback)
jsonpCallback:"call",//自定义的jsonp回调函数名称,默认为jQuery自动生成的随机函数名,也可以写"?",jQuery会自动为你处理数据
success: function(jsons){

console.log(jsons)
if(jsons.status==1){
alert("goods_id:"+jsons.goods_id+";\n面料编号:"+jsons.goods_name+";\n描述是:"+jsons.keywords+‘;\n基本价格是:‘+jsons.shop_price+"韩元")
}else if(jsons.status==0){
alert(jsons.data)
}
},
error: function(jsons){
//console.log(jsons)
alert(jsons.status+"\n路径错误")
}
});
}
window.onload=(jsonp());
</script>

PHP代码:

<?php
include_once "./radio_class.php";
$radio = new Radio();
$goods_name = $_GET["goods_name"];
//$goods_name = ‘Mvbb4AeP‘;
if(empty($goods_name)){
$arr = array(‘data‘=>‘参数为空‘,‘status‘=>0);
$results = json_encode($arr);
echo "call($results)";exit();
}
$table=‘ecs_goods‘;
$where=array(‘goods_name‘=>$goods_name);
$fieldes=array(‘goods_id‘,‘goods_name‘,‘keywords‘,‘shop_price‘);
$arr = $radio->fileds_select($table,$where,$fieldes);
//print_r($arr);
echo $arr;
exit();

附:radio_class.php

<?php
header(‘content-type:text/html;charset=utf-8‘);
class Radio{
private $host;
private $port;
private $user;
private $pass;
private $dbname;
private $charset;
private $link;
private static $db;

public function __construct($arr=array()){
$this->host = isset($arr[‘host‘]) ? $arr[‘host‘] : ‘localhost‘;
$this->port = isset($arr[‘port‘]) ? $arr[‘port‘] : ‘3306‘;
$this->user = isset($arr[‘user‘]) ? $arr[‘user‘] : ‘root‘;
$this->pass = isset($arr[‘pass‘]) ? $arr[‘pass‘] : ‘root‘;
$this->dbname = isset($arr[‘dbname‘]) ? $arr[‘dbname‘] : ‘newlibrary‘;
$this->charset = isset($arr[‘charset‘]) ? $arr[‘charset‘] : ‘utf8‘;
//连接数据库
$this->db_connect();
//选择数据库
$this->db_usedb();
//设置字符集
$this->db_charset();
}
public function __clone(){
trigger_error(‘克隆不被允许‘,E_USER_ERROR);
}
private function aaa(){
echo ‘nihao1‘;die;
}
public function __call($action,$b){
var_dump($action);var_dump($b);die;
}
public static function getInstance($arr){
if(!self::$db){
self::$db = new Self($arr);
}
return self::$db;
}
//连接数据库
private function db_connect(){
$this->link=mysqli_connect($this->host.‘:‘.$this->port , $this->user , $this->pass);
if(!$this->link){
echo "数据库连接失败 <br>";
echo "错误编码是".mysqli_errno().‘<br>‘;
echo "错误原因是".mysqli_error().‘<br>‘;
exit;
}
}
//选择数据库
private function db_usedb(){
mysqli_query($this->link,"use {$this->dbname}");
}
//设置字符集
private function db_charset(){
mysqli_query($this->link,"set names {$this->charset}");
}
/**
* 执行sql语句
* @param $sql
* @return source
**/
public function query($sql){
$res=mysqli_query($this->link,$sql);
if(!$res){
echo "SQL语句有错误 <br>";
echo "错误编码是".mysqli_errno($this->link).‘<br>‘;
echo "错误原因是".mysqli_error($this->link).‘<br>‘;
exit;
}
return $res;
}

/**
* 获取一条记录
* 前置条件 资源型
* @param $query
* @return array 二维
**/
public function getRowFromSourse($query,$type=‘assoc‘){
if(!in_array($type, array(‘assoc‘,‘array‘,‘row‘))){
die(‘转化错误‘);
}
$funcname="mysqli_fetch_".$type;
return $funcname($query);
}
/**
* 获取列表
* @param $sql
* @return array 二维
**/
public function getAll($sql){
$query=$this->query($sql);
$list=array();
$list = $this->getRowFromSourse($query);
/*while ($row=$this->getRowFromSourse($query)) {
$list[]=$row;
}*/
return $list;
}

/**
* 显示数据
* @param $table 表名
* @param $fieldes 字段名
* @param $where where条件
* @return array 二维数组
**/
public function fileds_select($table,$where=array(),$fieldes=array()){
$str=‘1=1‘;
$strs=‘‘;
if(is_array($where)){
foreach ($where as $key => $v) {
$str.=‘ and ‘.$key."=‘".$v."‘";
}
}
if(is_array($fieldes)){
foreach ($fieldes as $key => $v) {
$strs.=‘,‘.$v;
}
$strs = substr($strs,1);
}

$sql="select $strs from $table where $str";

$result = $this->getAll($sql);
if(empty($result)){
$arr = array(‘data‘=>‘数据为空‘,‘status‘=>0);
$result = json_encode($arr);
return "call($result)";
}
//return $result;die;
$result[‘status‘]=1;
$result = json_encode($result);
return "call($result)";

}
}

注:一定要在两个域名下测试哦

ajax实现jsonp跨域接口

标签:

原文地址:http://www.cnblogs.com/jingjingzhou/p/5629213.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!