2014-03-19 22 views
0

在這種SO post我學會了如何從AJAX調用得到一個返回值:jQuery的阿賈克斯調用從不調用方法

function CallIsDataReady(input) { 
      $.ajax({ 
       url: "http://www.blah.com/services/TestsService.svc/IsDataReady", 
       type: "GET", 
       contentType: "application/json; charset=utf-8", 
       data: input, 
       dataType: "json", 
       success: function (data) { 
        if (!data) { 
         setTimeout(function (inputInner) { CallIsDataReady(inputInner); }, 1000); 
        } 
        else { 
         //Continue as data is ready 
         callUpdateGrid(input); 
        } 
       } 
      }); 
     } 

     $(document).ready(function() { 
      var input = { requestGUID: "<%=guid %>" }; 

      CallIsDataReady(input); 

     }); 

該函數調用其Web服務至極不會返回true。問題是,以下callUpdateGrid內,記錄顯示,該Web服務方法是沒有得到從$就電話叫:

function callUpdateGrid(input) { 
    console.log(input); 
    $.ajax({ 
     url: "http://www.blah.com/services/TestsService.svc/GetContactsDataAndCountbyGUID", 
     type: "GET", 
     contentType: "application/json; charset=utf-8", 
     data: input, 
     dataType: "json", 
     success: function (data) { 
      var mtv = $find("<%= RadGrid1.ClientID %>").get_masterTableView(); 
         console.log(data); 
         mtv.set_dataSource(data.d.Data); 
         mtv.dataBind(); 
        } 
    }); 
} 

任何人都知道什麼是錯的?

+0

在頂層函數中,你有'callUpdateGrid(input)' - 不應該是'callUpdateGrid(data)'?? – Pointy

+0

數據不是第一次調用返回的真/假值嗎? – user2471435

+0

是的,我正在調用函數else { //繼續數據準備 callUpdateGrid(input); } – user2471435

回答

1

它是總是包含錯誤處理函數作爲傳遞給$ .ajax的選項之一的一個好主意。例如,在你成功的函數添加以下代碼:

, 
    error: function(jqXHR, textStatus, errThrown) { 
     console.log("AJAX call failed"); 
     console.log(errThrown); 
    } 

將登錄至少一點信息,如果$就調用不成​​功。

編輯 根據您的意見,這會將 SyntaxError: Invalid character

而事實上,我現在看到你給一個普通的JavaScript對象傳遞給$阿賈克斯的data選項,但表示它是dataType字段中的JSON對象。實際上你需要輸入對象轉換成JSON自己,就像這樣:

data: JSON.stringify(input), 
    dataType: 'json', 

或者,你可以簡單地格式化input在首位JSON對象,像這樣:

var input = { "requestGUID": "<%=guid %>" }; 

的引號在這種情況下,字段名稱requestGUID就足以爲您提供JSON對象。

+0

AJAX調用失敗SyntaxError:無效的字符它在CallDataIsReady – user2471435

+0

它必須位於callUpdateGrid上,因爲日誌記錄確認調用了第一個Web調用並且成功使用true。 – user2471435

+0

我改爲var input = {「requestGUID」:「<%=guid %>」};和我得到相同的確切錯誤 – user2471435