2012-08-02 28 views
3

這裏我試圖從REST API數據請求數據,並使用JQuery將其呈現在網頁中。JQuery回調方法中的不一致

我正在做3個不同的請求,大約需要200ms,300毫秒,7000毫秒的響應時間。這裏有兩個調用方法被稱爲3,我不明白爲什麼第三個不被調用。當使用Firebug工具進行調試時,響應正從服務器獲取。

當以下函數的順序改變時,行爲也發生了變化。任何一個或兩個回調方法正在被調用。

請幫我解決這個問題。

$.getJSON(
    "http://115.111.168.221:8080/AnalysisWebService/getDataFromSocialMedia?searchString=wellpoint&startDate=2012-08-01&endDate=2012-08-04&method=getAnalysisRange", 
    function(json) { 

     } 

$(function() { 
$.getJSON(
"http://115.111.168.221:8080/AnalysisWebService/getDataFromSocialMedia?searchString=wellpoint&source=facebook,twitter&searchFrom=socialmention&method=getTodayAnalysis",   

function(json) { 

} 
} 
); 
); 

$(function() { 
$.getJSON(
"http://115.111.168.221:8080/AnalysisWebService/getDataFromSocialMedia?searchString=wellpoint&source=facebook,twitter&searchFrom=socialmention&method=getCurrentAnalysis",  

function(json) { 

} 
} 
); 
); 

回答

1

試試這個:

var url = 'http://115.111.168.221:8080/AnalysisWebService/getDataFromSocialMedia?searchString=wellpoint'; 

$.getJSON(url + '&startDate=2012-08-01&endDate=2012-08-04&method=getAnalysisRange', function (json) { 
    // do something 
}); 

$.getJSON(url + '&source=facebook,twitter&searchFrom=socialmention&method=getTodayAnalysis', function (json) { 
    // do something 
}); 

$.getJSON(url + '&source=facebook,twitter&searchFrom=socialmention&method=getCurrentAnalysis', function (json) { 
    // do something 
}); 

你並不需要包起來什麼都因爲DOM沒有做好準備,以便你火了一些要求。

更重要的是,更多的可讀性做這樣的事情:

var url = 'http://115.111.168.221:8080/AnalysisWebService/getDataFromSocialMedia'; 

$.getJSON(url, { 
    searchString: 'wellpoint', 
    startDate: '2012-08-01', 
    endDate: '2012-08-04', 
    method: 'getAnalysisRange' 
}).success(function(json) { 
    console.log(json); 
}).error(function() { 
    console.log('error!'); 
}); 

$.getJSON(url, { 
    searchString: 'wellpoint', 
    source: 'facebook,twitter', 
    searchFrom: 'socialmention', 
    method: 'getTodayAnalysis' 
}).success(function(json) { 
    console.log(json); 
}).error(function() { 
    console.log('error!'); 
}); 

$.getJSON(url, { 
    searchString: 'wellpoint', 
    source: 'facebook,twitter', 
    searchFrom: 'socialmention', 
    method: 'getCurrentAnalysis' 

}).success(function(json) { 
    console.log(json); 
}).error(function() { 
    console.log('error!'); 
}); 

編輯:我已經附加錯誤處理爲好,並取消了?從網址。

+0

感謝您的幫助!我的代碼格式可以改進,並且我已經按照你的建議寫了。但是我的問題依然存在。我的回調沒有被要求1或2個上述請求。我沒有任何線索,爲什麼這是這樣的行爲。由於我提出了多個請求,會有什麼問題? – Swamy 2012-08-02 19:16:01

+0

嘗試添加錯誤處理程序,正如我在編輯中所做的那樣。他們在射擊嗎?你可能會得到一個服務器端錯誤,這就是爲什麼你的成功處理程序可能不會被解僱。 – Radu 2012-08-02 19:28:10

+0

我能夠使用Firebug插件查看響應。我測試了服務器端功能,它工作正常。還有一點需要提及,當我重複發送請求時,幾次我的回調方法被調用。我正在研究一個方向,它可以是同步問題嗎? – Swamy 2012-08-02 19:50:03

1

您的語法似乎是關閉的,括號和parens排列不正確。如果您通過Firebug進行檢查,您應該在頁面上收到錯誤消息。

更新: 我相信你的代碼格式應爲這樣:

$.getJSON(
    url, 
    function(json) { 

    } 
) 

$(function() { 
    $.getJSON(
    url,  

     function(json) { 

     } 
    ) 
} 
); 

$(function() { 
    $.getJSON(
     url,  

     function(json) { 

     } 
    ) 
} 
); 

此外,你應該考慮將重複的代碼放到一個功能,僅使用的document.ready調用之一。這將使您的代碼更健壯,更易於閱讀。

+2

您可以擁有儘可能多的document.ready,只要您希望AFAIK。 – Radu 2012-08-02 18:49:36

+0

@GrailsGuy不正確,可以根據需要添加$(function(){})聲明。 http://jsfiddle.net/5pVLG/ 所有這些方法將被添加到將調用document.ready – Vlad 2012-08-02 18:55:09