【发布时间】:2016-12-27 09:31:51
【问题描述】:
当我执行echo $data 时,我无法return $data 页面将加载(见箭头---> getPage.php)。
但我想将$data 返回到page1.php 我会得到{"html":null,"url":"floret media"} 你可以在这里看到"html":null 为什么会这样?当我将response tab 中的return $data 替换为echo $data 时,我看到了这个(全页)
> HTTP/1.1 200 OK Date: Tue, 27 Dec 2016 09:24:01 GMT Expires: -1
> Cache-Control: private, max-age=0 Content-Type: text/html;
> charset=ISO-8859-1 P3P: CP="This is not a P3P policy! See
> https://www.google.com/support/accounts/answer/151657?hl=en for more
> info." Server: gws X-XSS-Protection: 1; mode=block X-Frame-Options:
> SAMEORIGIN Set-Cookie:
> NID=93=XFlY_DE0XhLLdLDiTeHZi44J_z61nRxwTBInVkZlRVA3MsnuhB3iAqcR12lMs44dRI3dMWriif3USxysmiXDqm317uTYg08TzgYzFZa-VVgII_KE5fVsxueZcAmzlMCrOCw1LCSPCuIKfVI;
> expires=Wed, 28-Jun-2017 09:24:01 GMT; path=/; domain=.google.com;
> HttpOnly Accept-Ranges: none Vary: Accept-Encoding Transfer-Encoding:
> chunked
<!doctype html><html itemscope="" ><!--WHOLE PAGE --></html>
浏览器不允许返回响应调用ajax?
page1.php
var url = 'floret media';
$.ajax({
url:"getPage.php",
type:"POST",
dataType:"json",
data:{url:url},
success: function(data){
console.log(data);
}
});
getPage.php
function curl_google($keyword){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,
'http://www.google.com/search?hl=en&q='.urlencode($keyword).'&btnG=Google+Search&meta=');
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FILETIME, true);
$data = curl_exec($ch);
//ob_flush();//Flush the data here
if ($data === FALSE) {
echo "cURL Error: " . curl_error($ch);
}
curl_close($ch);
------> return $data; <---- // when i change it to echo $data it will work, but return $data; will not work i will get json response as {"html":null,"url":"floret media"}
}
$url = $_POST["url"];
$html = curl_google($url);
$response = json_encode(array("html"=>$html,"url"=>$url));
echo $response;
【问题讨论】:
-
在你的page1 data:{url:url)}中,应该是data:{url:url}, rt? ')' 你放置了额外的
-
@Amruthls,那是输入错误,我会纠正它