1
我試圖做一個通用函數,允許我在同一時間從不同的來源獲取數據。多個AJAX調用有錯誤
我基於this後我的解決方案,並結束了與此:
var servicesURL = 'http://www.somedomain.com/services/xml_proxy.php?url=';
function getExternalData(service, callback) {
$.ajax({
type: 'GET',
url: servicesURL+service,
dataType: 'jsonp',
jsonpCallback: 'myfunction',
success: function(data) { callback(data); },
error: function(jqXHR, textStatus, errorThrown) { console.log(textStatus+': '+errorThrown); }
});
}
getExternalData('data1.xml', function(data) {
console.log(data)
});
getExternalData('data2.xml', function(data) {
console.log(data)
});
下面是我使用代理服務器的代碼:
<?php
header('Content-type: application/json');
$url = $_GET['url'];
echo 'myfunction('.json_encode(simplexml_load_file($url)).')';
?>
它正常工作時我做一個單次呼叫該功能,但是當我多做一個呼叫時(如上所述),我收到以下錯誤:
parsererror:錯誤:myfunction未被調用
遺漏的類型錯誤:對象的特性「myfunction的」的翻譯:不是一個函數
任何幫助將高度讚賞
什麼,在哪裏到底是'回調()'在這個片段中定義的? – 2013-03-06 14:30:16
你真的需要jsonpCallback:'myfunction'....如果不是,那就把它刪除再試一次。 – Peeyush 2013-03-06 14:40:05
@ZlatanO。當getExternalData()函數被聲明爲它的第二個參數時,回調函數被定義,當函數執行時它會通過ajax調用接收到的數據。 – 2013-03-06 14:42:40