2013-02-21 85 views
-1

在我的javascript我有,

$.ajax({ 
        type: 'GET', 
        url: 'http://133.333.33.33/reporting/summary_table.php?loc_id='+locid+'&loc_type='+loctype+'', 
        async: false, 
        success: function(data) { 
         alert(data); 
        }, 
        dataType: 'json' 
       }); 

在我的服務器端我有這個,

$result = mysql_query($query); 
$rows = array(); 
while ($row = mysql_fetch_assoc($result)) { 
    $rows[] = $row; 
} 
echo json_encode($rows); 

當我檢查我的FF螢火蟲,Ajax響應是什麼,它激發出一個錯誤,而不是。我錯過了什麼?

+3

除非服務器支持並啓用CORS,否則無法進行跨域Ajax調用。或者你必須使用JSONP,它也必須得到服務器的支持。 – 2013-02-21 17:28:49

+2

「它會發出錯誤,而不是」 - **什麼**錯誤? – Quentin 2013-02-21 17:29:33

+0

您正在使用[an **過時的**數據庫API](http://stackoverflow.com/q/12859942/19068),並應使用[現代替換](http://php.net/manual/en/ mysqlinfo.api.choosing.php)。 – Quentin 2013-02-21 17:30:34

回答

0

打電話給你的服務如下:

$.ajax({ 
url: 'http://yourserver.com/reporting/summary_table.php?loc_id='+locid+'&loc_type='+loctype+'', 
success: function(data) { 
alert(data); 
}, 
dataType: 'jsonp' 
}); 

當心「JSONP」數據類型。

然後實現在你的服務器端腳本這個小變化:

$jsonp = json_encode($rows); 
if(isset($_GET["callback"])) $jsonp = $_GET["callback"]."($jsonp)"; 
echo $jsonp; 

所以你的PHP這會是能夠回答JSONP請求。