多个AJAX调用错误

Multiple AJAX calls with errors

本文关键字:错误 调用 AJAX 多个      更新时间:2023-09-26

我想做一个通用的函数,让我在同一时间从不同的来源获得数据。

我的解决方案基于这篇文章,并以这样结束:

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: Error: myfunction was not called

Uncaught TypeError: Property 'myfunction' of object [object object] is not a function

如有任何帮助,不胜感激

尝试将第二个调用放在第一个回调的内部。这应该能解决你遇到的问题。

http://forum.jquery.com/topic/multiple-jsonp-requests-causing-errors