2011-03-31 93 views
0

我在尋找一個簡單的腳本中,我可以做這樣的事情死簡單的跨域PHP腳本

$.getScript('fetcher.php?url=' + escape('http://www.google.com') + '&callback=console.log'); 

的反應應該是一個很長的線,看起來像這樣:

console.log({responseText: '<!doctype html><html><head><meta http-equiv="content-type" content="text/html; charset=UTF-8"><title>Google</title><script>windo...'}) 

它不應該超過10行代碼,有沒有辦法不存在。

我在XAMPP中使用php,我只是用它來建立一個數據庫,所以我不需要任何裝飾(沒有得到vs post,沒有數據包括)只是file_get_contents$_GET。當然,我還是想對URL進行編碼

+0

你限制每行字符?如果不要,我可以寫在1線:d – thaolt 2011-03-31 02:39:37

+0

@thaolt:如何可讀的劇本,只是沒有太激烈的選項 – qwertymk 2011-03-31 02:41:24

+0

此鏈接描述了這一問題非常好:http://bit.ly/eDLd4S – 2011-03-31 06:56:08

回答

1

這個怎麼樣,更新

<?php 
    // fetcher.php 
    $url = $_GET['url']; 
    $callback = $_GET['callback']; 
    $read = file_get_contents($url); 
    $read = addslashes(htmlspecialchars(str_replace("\n","\\n",$read))); 
?> 
<script> 
    <?php echo $callback ?>({responseText: '<?php echo $read; ?>'}); 
</script> 
+0

我想保留'「\ n''(HTML中的換行符),成爲'」 \\ n''(在響應中)所以在字符串中它是一個換行符。此外,我婉能夠使用轉義網址 – qwertymk 2011-03-31 02:57:28

+0

我沒有測試這個呢,你嘗試dakis的代碼,虐待測試出來 – thaolt 2011-03-31 03:02:44

+0

我沒有理會,我沒有捲曲 – qwertymk 2011-03-31 03:04:01

0

fetcher.php

<?php 
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($ch, CURLOPT_URL, $_GET['url']); 
    echo curl_exec($ch); 
    curl_close($ch); 
?> 

的JavaScript

$.get("fetcher.php", {url: "http://www.google.com/"}, function(response) { 
    console.log(response); 
});