ajax返回api信息带头信息和不带头信息(只返回内容信息)
ajax返回带有头信息的内容
public function chejiahaochaxun(){
$chejiahao=I("chejiahao","","strip_tags");
$host = "ali-vin.showapi.com";
$path = "/vin";
$method = "GET";
$appcode = "8a86e0cd2b854987b058a3e********";
$headers = array();
array_push($headers, "Authorization:APPCODE " . $appcode);
$querys = "vin=".$chejiahao;
$bodys = "";
$url = $host . $path . "?" . $querys;
$curl = curl_init();
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_FAILONERROR, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, true);
if (1 == strpos("$".$host, ""))
{
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
}
$contents = curl_exec($curl);
//var_dump(curl_exec($curl));
$this->ajaxReturn(curl_exec($curl));//返回带有头部信息的内容
}
ajax返回不带头信息的内容
public function chejiahaochaxun(){
$chejiahao=I("chejiahao","","strip_tags");
$host = "ali-vin.showapi.com";
$path = "/vin";
$method = "GET";
$appcode = "8a86e0cd2b854987b058a3eb******";
$headers = array();
array_push($headers, "Authorization:APPCODE " . $appcode);
$querys = "vin=".$chejiahao;
$bodys = "";
$url = $host . $path . "?" . $querys;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$info = curl_exec($ch);
if (curl_errno($ch)) {
ajaxReturn('Errno'.curl_error($ch));
}
curl_close($ch);
$this->ajaxReturn($info); //返回不带头部信息的内容,直接返回数据内容
//var_dump(curl_exec($curl));
//$this->ajaxReturn(curl_exec($curl));
}
前端的js直接解析ajax返回的数据。
$.ajax({
url:"__CONTROLLER__/chejiahaochaxun",//这里指向的就不再是页面了,而是一个方法。
data:{chejiahao:chejiahao},
type:"POST",
dataType:"JSON",
success: function(data){
//alert(typeof(data));
var obj = eval('(' + data + ')');
msgbody=obj.showapi_res_body;
$("#brand_name").text(msgbody.brand_name);
$("#model_name").text(msgbody.model_name);
$("#car_type").text(msgbody.car_type);
$("#sale_name").text(msgbody.sale_name);
$("#manufacturer").text(msgbody.manufacturer);
$("#engine_type").text(msgbody.engine_type);
$("#effluent_standard").text(msgbody.effluent_standard);
$("#drive_style").text(msgbody.drive_style);
$("#output_volume").text(msgbody.output_volume);
$("#power").text(msgbody.power);
$("#seat_num").text(msgbody.seat_num);
$("#made_year").text(msgbody.made_year);
$("#made_month").text(msgbody.made_month);
$("#stop_year").text(msgbody.stop_year);
$("#fuel_num").text(msgbody.fuel_num);
$("#transmission_type").text(msgbody.transmission_type);
$("#cylinder_number").text(msgbody.cylinder_number);
$("#guiding_price").text(msgbody.guiding_price);
$("#fuel_Type").text(msgbody.fuel_Type);
$("#car_body").text(msgbody.car_body);
$("#door_num").text(msgbody.door_num
);
// sale_name=msgbody.sale_name;
//alert(sale_name);
// $("#chejiahaoinfo").text(data);
dialog.modal('hide');
}
})